【Spring】Spring 3.x企业应用开发实战(7)—-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

直接实例:

 

package com.techman.context;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.techman.reflect.Car;



@Configuration //表示是一个配置信息提供类,这里是通过类注解的配置方式
public class Beans 
{
	@Bean(name="car")
	public Car buildCar()
	{
		Car car=new Car();
		car.setBrand("红旗CA72");
		car.setMaxSpeed(340);
		
		return car;
	}
}

 

package com.techman.context;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.techman.reflect.Car;
//这里需要spring-context.jar和spring-expression.jar的支持
public class AnnotationApplicationContext 
{
	public static void main(String []args)
	{
		//通过一个带@Configuration的POJO装载Bean配置
		ApplicationContext ac=new AnnotationConfigApplicationContext(Beans.class);
		Car car=ac.getBean("car",Car.class);
		car.introduce();
	}
}

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的配置:

 

<!-- 从类路径下加载Spring配置文件,classpath关键字特指类路径下加载 ,如果多个文件则使用逗号或空格隔开-->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml,/WEB-INF/techman-dao.xml</param-value>  
  </context-param>
  
  <!-- 负责启动Spring容器的监听器,它将引用上面的上下文参数获得Spring配置文件地址 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

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

 

 

<!-- 从类路径下加载Spring配置文件,classpath关键字特指类路径下加载 ,如果多个文件则使用逗号或空格隔开-->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml,/WEB-INF/techman-dao.xml</param-value>  
  </context-param>

 

 

  <servlet>
  	<servlet-name>springContextLoaderServlet</servlet-name>
  	<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>

 

6、Log4j的配置

 

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

 

<context-param>
  	<param-name>log4jConfigLocation</param-name>
  	<param-value>/WEB-INF/log4j.properties</param-value>  
  </context-param>
  <servlet>
  	<servlet-name>log4jConfigServlet</servlet-name>
  	<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>

 

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

方式如下:

<!-- 通过指定context参数,让Spring使用AnnotationConfigWebApplicationContext而非XmlWebApplicationContext启动容器 -->
  <context-param>
  	<param-name>contextClass</param-name>
  	<param-value>
  		org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  	</param-value>
  </context-param>
  
  <!-- 指定标注了@Configuration的配置类,多个可以使用逗号或空格分隔 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>com.smart.Beans,com.smart.AppConfig2</param-value>
  </context-param>
  
  <!-- ContextLoaderListener监听器将根据上面的配置使用AnnotationConfigWebApplicationContext根據contextConfigLocation指定的配置類啟動Spring容器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

 

 

 

 

 

 

 

 

 


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