Spring中加载ApplicationContext.xml的几种方式

Spring中加载ApplicationContext.xml的几种方式

  • 直接创建ApplicationContext上下文对象加载配置文件

    ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
    ac.getBean():

 

  • 创建类ContextLoaderListener实现ServletContextListener,加载配置文件。
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        //将spring的应用上下文对象存储到最大域ServletContext中
        ServletContext servletContext = servletContextEvent.getServletContext();
        ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        servletContext.setAttribute("ac",ac);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

web.xml

  <listener>
    <listener-class>com.itheima.listener.ContextLoaderListener</listener-class>
  </listener>
  • 通过web.xml创建全局初始化参数,通过监听器监听服务器启动时加载配置文件。

ContextLoaderListener.java

public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        //将spring的应用上下文对象存储到最大域ServletContext中
        ServletContext servletContext = servletContextEvent.getServletContext();
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext ac=new ClassPathXmlApplicationContext(contextConfigLocation);
        servletContext.setAttribute("ac",ac);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

WebApplicationContextUtils.java

public class WebApplicationContextUtils {

    public static ApplicationContext getApplicationContext(ServletContext servletContext){
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext ac=new ClassPathXmlApplicationContext(contextConfigLocation);
        return ac;
    }
}

web.xml

  <listener>
    <listener-class>com.itheima.listener.ContextLoaderListener</listener-class>
  </listener>

  <!--全局初始化参数-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
  <servlet>

 通过spring-web提供的监听器监听服务器启动并读取配置文件

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.save();
    }
}

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中, 提供了一个客户端工具WebApplicationContextUtils供使用者获得应用.上下文对象。所以我们需要做的只有两件事:
①在web.xmI中配置ContextLoaderListener监听器 (导入spring-web坐标)
②使用WebApplicationContextUtils获得应用 上下文对象ApplicationContext

 


版权声明:本文为qq_27151401原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>