Spring--ApplicationContext

各种继承层级

  • 最顶层是 BeanFactory 接口,具有getBean()等方法。
  • ListableBeanFactory 接口继承了 BeanFactory 接口
  • ApplicationContext 接口继承了 ListableBeanFactory 接口。不仅具备BeanFactory的功能,还添加了getResourceByPath方法,具备了ResourceLoader读入以Resource定义的BeanDefinition的能力。
BeanFactory 提供了最基本的IOC容器的功能,是IOC容器的基本形式,而各种ApplicationContext的实现是IOC容器的高级表现形式。

ApplicationContext 和 BeanFactory都能够获取getBean,区别在于:

  • BeanFactory在初始化容器时,并未实例化Bean,直到第一次访问某个Bean时才实例目标Bean;
  • ApplicationContext则在初始化应用上下文时就实例化所有单实例的Bean。因此ApplicationContext的初始化时间会比BeanFactory稍长一些,不过稍后的调用则没有"第一次惩罚"的问题。

ApplicationContext 的实现类

  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
当然FileSystemXmlApplicationContext和ClassPathXmlApplicationContext都可以显式使用带资源类型(classpath:或file:)前缀的路径,它们的区别在于如果不显式指定资源类型前缀,将分别将路径解析为文件系统路径和类路径罢了。
  • WebApplicationContext(XmlWebApplicationContext) 是专门为Web应用准备的,它允许从相对于Web根目录的路径中装载配置文件完成初始化工作。
  • WebApplicationContext的初始化需要ServletContext实例,也就是说它必须在拥有Web容器的前提下才能完成启动的工作。

WebApplicationContext

  • 有过Web开发经验的读者都知道可以在web.xml中配置自启动的Servlet或定义Web容器监听器(ServletContextListener),借助这两者中的任何一个,我们就可以完成启动Spring Web应用上下文的工作。
  • Spring分别提供了用于启动WebApplicationContext的Servlet和Web容器监听器:org.springframework.web.context.ContextLoaderServlet; , org.springframework.web.context.ContextLoaderListener。两者的内部都实现了启动WebApplicationContext实例的逻辑,我们只要根据Web容器的具体情况选择两者之一,并在web.xml中完成配置就可以了。
  • 下面是使用ContextLoaderListener启动WebApplicationContext的具体配置:
<!--①指定配置文件--> 
<context-param>                                                        
     <param-name>contextConfigLocation</param-name>   
     <param-value> 
       /WEB-INF/baobaotao-dao.xml, /WEB-INF/baobaotao-service.xml  
   </param-value> 
</context-param> 
<!--②声明Web容器监听器--> 
<listener>   
     <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> 
</listener> 
  • ContextLoaderListener通过Web容器上下文参数contextConfigLocation获取Spring配置文件的位置。用户可以指定多个配置文件,用逗号、空格或冒号分隔均可。对于未带资源类型前缀的配置文件路径,WebApplicationContext默认这些路径相对于Web的部署根路径。当然,我们可以采用带资源类型前缀的路径配置,如"classpath:/baobaotao-.xml"和上面的配置是等效的。
  • 如果在不支持容器监听器的低版本Web容器中,我们可采用ContextLoaderServlet完成相同的工作:
<context-param> 
     <param-name>contextConfigLocation</param-name>   
     <param-value>/WEB-INF/baobaotao-dao.xml, /WEB-INF/baobaotao-service.xml </param-value> 
</context-param> 
…  
<!--①声明自动启动的Servlet --> 
<servlet>   
    <servlet-name>springContextLoaderServlet</servlet-name> 
    <servlet-class>org.springframework.web.context.ContextLoaderServlet </servlet-class> 
    <!--②启动顺序--> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
  • 由于WebApplicationContext需要使用日志功能,用户可以将Log4J的配置文件放置到类路径WEB-INF/classes下,这时Log4J引擎即可顺利启动。如果Log4J配置文件放置在其他位置,用户还必须在web.xml指定Log4J配置文件位置。Spring为启用Log4J引擎提供了两个类似于启动WebApplicationContext的实现类:Log4jConfigServlet和Log4jConfigListener,不管采用哪种方式都必须保证能够在装载Spring配置文件前先装载Log4J配置信息。
  • 指定Log4J配置文件时启动Spring Web应用上下文:
<context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
           /WEB-INF/baobaotao-dao.xml,/WEB-INF/baobaotao-service.xml  
        </param-value> 
    </context-param> 
     <!--①指定Log4J配置文件位置--> 
    <context-param>                                             
        <param-name>log4jConfigLocation</param-name> 
        <param-value>/WEB-INF/log4j.properties</param-value> 
    </context-param> 
 
    <!--②装载Log4J配置文件的自启动Servlet --> 
    <servlet> 
        <servlet-name>log4jConfigServlet</servlet-name> 
        <servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet> 
        <servlet-name> springContextLoaderServlet</servlet-name> 
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
  • 注意上面我们将log4jConfigServlet的启动顺序号设置为1,而springContextLoaderServlet的顺序号设置为2。这样,前者将先启动,完成装载Log4J配置文件初始化Log4J引擎的工作,紧接着后者再启动。如果使用Web监听器,则必须将Log4jConfigListener放置在ContextLoaderListener的前面。
  • 采用以上的配置方式Spring将自动使用 XmlWebApplicationContext 启动Spring容器,即通过XML文件为Spring容器提供Bean的配置信息。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容