zoukankan      html  css  js  c++  java
  • 第52周四ApplicationContext

    ApplicationContext的中文意思是“应用前后关系”应用上下文即容器,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持,被推荐为Java EE应用之首选在ApplicationContext接口的众多实现类中,有3个是我们经常用到的(见表1-1),并且使用这3个实现类也基本能满足我们Java EE应用开发中的绝大部分需求。
    表1-1 ApplicationContext接口的常用实现类介绍
    类 名 称
    功 能 描 述
    ClassPathXmlApplicationContext
    从类路径ClassPath中寻找指定的XML配置文件,找到并装载
    完成ApplicationContext的实例化工作。例如:
    //装载单个配置文件实例化ApplicationContext容器
    ApplicationContext cxt = new ClassPathXmlApplicationContext
    ("applicationContext.xml");
    //装载多个配置文件实例化ApplicationContext容器
    String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
    ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
    FileSystemXmlApplicationContext
    从指定的文件系统路径中寻找指定的XML配置文件,找到并装载
    完成ApplicationContext的实例化工作。例如:
    //装载单个配置文件实例化ApplicationContext容器
    ApplicationContext cxt = new FileSystemXMLApplicationContext
    ("beans.xml");
    //装载多个配置文件实例化ApplicationContext容器
    String[] configs = {"c:/beans1.xml","c:/beans2.xml"};
    ApplicationContext cxt = new FileSystemXmlApplicationContext(configs);
    XmlWebApplicationContext
    从Web应用中的寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。这是为Web工程量身定制的,使用WebApplicationContextUtils类的getRequiredWebApplicationContext方法可在JSP与Servlet中取得IoC容器的引用

    在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext,先让我们看看在Web应用中,怎么初始化WebApplicationContext,在web.xml中定义:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    -->

    可以看出,有两种方法,一个是用ContextLoaderListener这个Listerner,另一个是ContextLoaderServlet这个Servlet,这两个方法都是在web应用启动的时候来初始化WebApplicationContext,我个人认为Listerner要比Servlet更好一些,因为Listerner监听应用的启动和结束,而Servlet得启动要稍微延迟一些,如果在这时要做一些业务的操作,启动的前后顺序是有影响的。

    总结获取ApplicationContext的方法:

    方法一:在初始化时保存ApplicationContext对象
    代码:
    ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
    ac.getBean("beanId");
    说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

    方法二:通过Spring提供的工具类获取ApplicationContext对象
    代码:
    import org.springframework.web.context.support.WebApplicationContextUtils;
    ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
    ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
    ac1.getBean("beanId");
    ac2.getBean("beanId");
    说明:
    这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

    上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

    其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    方法三:继承自抽象类ApplicationObjectSupport
    说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
    Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

    方法四:继承自抽象类WebApplicationObjectSupport
    说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

    方法五:实现接口ApplicationContextAware
    说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
    Spring初始化时,会通过该方法将ApplicationContext对象注入。

      



  • 相关阅读:
    cogs 1682. [HAOI2014]贴海报 WW
    cogs 2039. 树的统计
    cogs luogu [NOIP2011] 选择客栈
    cogs luogu 1804. [NOIP2014]联合权值 WD
    cogs luogu [NOIP2014]生活大爆炸版石头剪刀布
    leetcode[119]Pascal's Triangle II
    leetcode[120]Triangle
    leetcode[121]Best Time to Buy and Sell Stock
    leetcode[122]Best Time to Buy and Sell Stock II
    leetcode[123]Best Time to Buy and Sell Stock III
  • 原文地址:https://www.cnblogs.com/doit8791/p/4185833.html
Copyright © 2011-2022 走看看