zoukankan      html  css  js  c++  java
  • Spring+struts2项目启动时加载applicationContext.xml并获取 ApplicationContext对象

    首先web.xml中加入如下监听器和上下文配置

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>

     Action方法中修改如下:

    public String execute() throws Exception {
           
            System.out.println("userAction...execute....");
            /*ApplicationContext context = 
                    new ClassPathXmlApplicationContext("applicationContext.xml");*/
            HttpServletRequest request = ServletActionContext.getRequest();
            
            WebApplicationContext context = WebApplicationContextUtils.
                getRequiredWebApplicationContext(request.getServletContext());
            
            
            UserService userService = (UserService)context.getBean("userService");
            userService.add();
            return NONE;
        }    
  • 相关阅读:
    「CF1039D」You Are Given a Tree
    「NOIP2016」换教室
    「NOIP2014」飞扬的小鸟
    「AMPPZ2014」The Prices
    POj-3104 Drying 二分+贪心
    HDOJ1312<DFS>
    STL入门2
    HDU1425 <sort 快排>
    2304: Lights Out(枚举)
    1018:放苹果(递归)
  • 原文地址:https://www.cnblogs.com/annofyf/p/9774542.html
Copyright © 2011-2022 走看看