ApplicationContext的名称解释

如果说BeanFactory是Spring的心脏,那么Application就是完整的身躯。ApplicationContext就是由BeanFactory派生出来的。

1、ApplicationContext

ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统加载文件。

如果配置文件放在类路径下,直接使用ClassPathXmlApplicationContext实现类:

ApplicationContext ctx=new ClassPathXmlApplicationContext("com/techman/context/beans.xml");

这里的参数等同于:"classpath:com/techman/context/beans.xml"

如果配置文件在文件系统的路径下,则可以优先考虑使用FileSystemXmlApplicationContext实现类:

ApplicationContext ctx=new FileSystemXmlApplicationContext("com/techman/context/beans.xml");

这里的参数等同于:"file:com/techman/context/beans.xml".

还可以指定一组配置文件,Spring自动将多个配置文件在内存中整合成一个配置文件:

ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"conf/bean1.xml","conf/bean2.xml"});

 

2、AnnotationConfigApplicationContext

直接实例:

 

  1. [java] view plaincopyprint?
  2. package com.techman.context;  
  3.   
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6.   
  7. import com.techman.reflect.Car;  
  8.   
  9.   
  10.   
  11. @Configuration //表示是一个配置信息提供类,这里是通过类注解的配置方式  
  12. public class Beans   
  13. {  
  14.     @Bean(name="car")  
  15.     public Car buildCar()  
  16.     {  
  17.         Car car=new Car();  
  18.         car.setBrand("红旗CA72");  
  19.         car.setMaxSpeed(340);  
  20.           
  21.         return car;  
  22.     }  
  23. }  
复制代码





  1. [java] view plaincopyprint?
  2. package com.techman.context;  
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  6.   
  7. import com.techman.reflect.Car;  
  8. //这里需要spring-context.jar和spring-expression.jar的支持  
  9. public class AnnotationApplicationContext   
  10. {  
  11.     public static void main(String []args)  
  12.     {  
  13.         //通过一个带@Configuration的POJO装载Bean配置  
  14.         ApplicationContext ac=new AnnotationConfigApplicationContext(Beans.class);  
  15.         Car car=ac.getBean("car",Car.class);  
  16.         car.introduce();  
  17.     }  
  18. }  
复制代码




AnnotationConfigApplicationContext将加载Beans.class中的Bean定义并调用Beans.class中实现的方法实例化Bean,启动容器并装配Bean.
3、WebApplicationContext

WebApplicationContext是专门为Web应用准备的,它允许从相对于web根目录的路径中加载配置文件完成初始化工作。


WebApplicationContext扩展了ApplicationContext,WebApplicationContext定义了一个常量ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,在上下文启动时,我们可以直接通过下面的语句从web容器中获取WebApplicationContext:

WebApplicationContext wac=(WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

4、ConfigurableWebApplicationContext

ConfigurableWebApplicationContext扩展了WebApplicationContext,它允许通过配置的方式实例化WebApplicationContext,它定义了两个重要的方法:

setServletContext(ServletContext servletContext):为Spring设置Web应用上下文,以便两者整合。

setConfigLocation(String[] configLocations)设置Spring配置文件地址,一般情况下,配置文件地址是相对于Web根目录的地址,如/WEB-INF/techman-dao.xml等。也可以使用classpath:com/techman/context/techman-dao.xml等格式。

 

5、Spring为使用WebApplicationContext的Servlet和Web容器监听器:

org.springframework.web.context.ContextLoaderServlet;

org.springframework.web.context.ContextLoaderListener;

这里是web.xml启动WebApplicationContext的配置:

 

  1. [html] view plaincopyprint?
  2. <!-- 从类路径下加载Spring配置文件,classpath关键字特指类路径下加载 ,如果多个文件则使用逗号或空格隔开-->  
  3.   <context-param>  
  4.     <param-name>contextConfigLocation</param-name>  
  5.     <param-value>classpath:applicationContext.xml,/WEB-INF/techman-dao.xml</param-value>    
  6.   </context-param>  
  7.     
  8.   <!-- 负责启动Spring容器的监听器,它将引用上面的上下文参数获得Spring配置文件地址 -->  
  9.   <listener>  
  10.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  11.   </listener>  
复制代码





如果在不支持容器监听器的低版本Web容器中,我们可采用ContextLoaderServlet完成相同的工作:

 

  1. [html] view plaincopyprint?
  2. <!-- 从类路径下加载Spring配置文件,classpath关键字特指类路径下加载 ,如果多个文件则使用逗号或空格隔开-->  
  3.   <context-param>  
  4.     <param-name>contextConfigLocation</param-name>  
  5.     <param-value>classpath:applicationContext.xml,/WEB-INF/techman-dao.xml</param-value>    
  6.   </context-param>  
复制代码





 

  1. [html] view plaincopyprint?
  2. <servlet>  
  3.     <servlet-name>springContextLoaderServlet</servlet-name>  
  4.     <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  
  5.     <load-on-startup>1</load-on-startup>  
  6. </servlet>  
复制代码





6、Log4j的配置

由于WebApplicationContext需要使用日志功能,用户可以将log4j.properties放置在类路径下,这样就会自动启动。如果放在其他地方,必须在web.xml中指定Log4j配置文件的位置。Spring为启动Log4j引擎提供了两个类og4jConfigServlet和Log4jConfigListener,不管哪种方式都必须保证能够在装载Spring配置文件前先装载Log4J配置信息。

 

  1. [html] view plaincopyprint?
  2. <context-param>  
  3.     <param-name>log4jConfigLocation</param-name>  
  4.     <param-value>/WEB-INF/log4j.properties</param-value>    
  5.   </context-param>  
  6.   <servlet>  
  7.     <servlet-name>log4jConfigServlet</servlet-name>  
  8.     <servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>  
  9.     <load-on-startup>1</load-on-startup>  
  10.   </servlet>  
复制代码





7、使用标注@Configuration的Java类提供配置信息

方式如下:

  1. [html] view plaincopyprint?
  2. <!-- 通过指定context参数,让Spring使用AnnotationConfigWebApplicationContext而非XmlWebApplicationContext启动容器 -->  
  3.   <context-param>  
  4.     <param-name>contextClass</param-name>  
  5.     <param-value>  
  6.         org.springframework.web.context.support.AnnotationConfigWebApplicationContext  
  7.     </param-value>  
  8.   </context-param>  
  9.     
  10.   <!-- 指定标注了@Configuration的配置类,多个可以使用逗号或空格分隔 -->  
  11.   <context-param>  
  12.     <param-name>contextConfigLocation</param-name>  
  13.     <param-value>com.smart.Beans,com.smart.AppConfig2</param-value>  
  14.   </context-param>  
  15.     
  16.   <!-- ContextLoaderListener监听器将根据上面的配置使用AnnotationConfigWebApplicationContext根據contextConfigLocation指定的配置類啟動Spring容器 -->  
  17.   <listener>  
  18.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  19.   </listener>  
复制代码


http://bbs.codeweb.cn/thread-5887-1-1.html

posted on 2017-12-12 14:48  admingy  阅读(897)  评论(0编辑  收藏  举报

导航