spring3笔记5-BeanFactory和ApplicationContext

来源:互联网 发布:软件企业财务管理制度 编辑:程序博客网 时间:2024/05/29 11:50

spring通过配置文件描述Bean和Bean之间的依赖关系,利用java语言的反射功能实例化Bean并建立Bean之间的依赖关系。这些底层的工作完成后,还提供了Bean实例缓存、生命周期管理、Bean实例代理、事件发布、资源装载等高级服务。

BeanFactory是spring框架最核心的接口,是spring的基础设施,面向spring本身,它提供了高级IOC的配置机制,BeanFactory使管理不同的java对象成为可能。

ApplicationContext建立在BeanFactory基础之上,面向使用spring框架的开发者,它提供能更多面向应用的功能,提供了国际化支持和框架事件体系,更易于创建实际应用,几乎所有应用场合我们都直接使用ApplicationContext而非底层的BeanFactory。

BeanFactory(Bean工厂:com.springframework.beans.factory.BeanFactory)

BeanFactory最主要的方法是getBean(String beanName),spring为BeanFactory提供了多种实现,最常用的是XMLBeanFactory。以下是BeanFactory相关扩展:

ListableBeanFactory:该接口定义了访问容器中Bean基本信息的若干方法,如查看bean的个数、获取某一类型bean的配置名、查看容器中是否包含某个bean等

HierarchicalBeanFactory:父子级联IoC容器接口,子容器可以通过接口访问父容器

ConfigurableBeanFactory:该接口增强了IoC容器的可定制性,他定义了设置类装载器、属性编辑器、容器初始化后置处理器等方法

AutowireCapableBeanFactory:定义了将容器中的bean按某种规则(如按名字匹配、按类型匹配等)进行自动装配的方法

SingletonBeanRegistry:定义了允许在运行期间向容器注册单例Bean的方法

BeanDefinitionRefistry:spring配置文件中每一个<bean>节点元素在spring容器里都通过一个BeanDefinition对象表示,他描述了Bean的配置信息。而该接口提供了向容器手动注册BeanDefinition对象的方法

DefaultSingletonBeanRegistry:该接口提供一个用于缓存单例Bean的缓存器,它用HashMap实现的缓存器,单例的Bean以beanName为键保存在这个map中

实例(bean.xml和test.java):

<beans><bean id="car1" class="com.taobao.Car"><property name="brand" value="红旗CA72" /><property name="color" value="黑色" /><property name="maxSpeed" value="300" /></bean><bean id="car2" class="com.taobao.Car" p:brand="红旗CA72" p:color="红色" p:maxSpeed="300" /></beans>

//实例化资源访问器(支持Ant风格)ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();//通过类路径访问资源Resource res = resolver.getSource("classpath:com/taobao/spring/bean.xml");//通过资源创建Bean工厂BeanFactory bf = new XmlBeanFactory(res);//获取bean节点对应的java对象Car car1 = bf.getBean("car1", Car.class);Car car2 = bf.getBean("car2", Car.class);

ApplicationContext(上下文:com.springframework.context.ApplicationContext)

ApplicationContext主要实现类:ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext,

//使用 ClassPathXmlApplicationContext 时,“com/taobao/spring/beans.xml” 等同于 “classpath:com/taobao/spring/beans.xml”ApplicationContext ctx = new ClassPathXmlApplicationContext("com/taobao/spring/beans.xml");//使用 FileSystemXmlApplicationContext 时,“com/taobao/spring/beans.xml” 等同于 “file:com/taobao/spring/beans.xml”ApplicationContext ctx = new FileSystemXmlApplicationContext("com/taobao/spring/beans.xml");//还可以指定一组配置文件,spring会自动将多个配置文件在内存中“整合”成一个配置文件,如下:ApplicationContext ctx = new ClassPathXmlApplicationContext( new String[]{ "conf/beans1.xml", "conf/beans2.xml" } );

具体实现接口:

ApplicationEventPublisher:让容器拥有发布应用上下文事件的功能,包括容器启动事件、关闭事件等。实现了ApplicationListener事件监听接口的Bean可以接收到容器事件,并对事件进行响应处理。在ApplicationContext抽象实现类AbstractApplicationContext中,我们可以发现存在一个ApplicationEventMulticaster,它负责保存所有监听器,以便在容器产生上下文事件时通知这些事件监听者。

MessageSource:为应用提供il8n国际化消息访问功能

ResourcePatternResolver:所有ApplicationContext实现类都实现了类似PathMatchingResourcePattern的功能,可以通过带前缀的Ant风格的资源文件路径装载spring的配置文件

LifeCyCle:该接口提供start() 和 stop() 两个方法,主要用于控制异步处理过程。

ConfingurableApplicationContext 扩展于ApplicationContext,它新增了两个方法:refresh() 和 close() 让ApplicationContext具有启动、刷新和关闭应用上下文的能力

AnnotationConfigApplicationContext 是spring为基于注解类的配置提供了专门的ApplicationContext的实现类,Spring3.0支持基于注解的配置方式。

//@Configuration注解的POJO即可提供Spring所需的Bean配置信息@Configurationpublic class Beans{//定义一个Bean@Bean(name="car")public Car buildCar(){Car car = new Car();car.setBrand("红旗CA72");car.setMaxSpeed(300);return car;}}

//通过一个带@Configuration的POJO装载Bean配置ApplicationContext ctx = new AnnotationConfigApplicationContext ("Beans.class");Car car = ctx.getBean("car", Car.class);

WebApplicationContext:

WebApplicationContext是专门为Web应用准备的,它允许从相对于Web根目录的路径中装载配置文件完成初始化工作。从WebApplicationContext中可以获得servletContext的引用,整个Web应用上下文对象将作为属性放在ServlertContext中,以便Web应用环境可以访问spring上下文。Spring专门为此提供一个工具类WebApplicationContextUtils,通过该类的方法getWebApplicationContext(ServletContext sc)方法,既可以从ServletContext中获取WebApplicationContext实例。

Spring2.0在WebApplicationContext中还为Bean添加了三个新的作用域:request作用域、session作用域和 global session作用域,在非Web应用的环境下,Bean只有singleton 和 prototype 两个作用域

WebApplicationContext定义了一个常量 ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,在上下文启动时,WebApplicationContext 实例即以此为键放置在ServletContext的属性列表,因此可以直接获取。

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

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

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

setConfigLocations(String[] configLocation):设置Spring配置文件地址,一般情况下,配置文件地址是相对Web根目录的地址,如/WEB-INF/bean.xml。也可以使用带资源类型前缀的地址,如classpath:com/taobao/bean.xml等

WebApplicationContext初始化

在web.xml中配置自启动的Servlet或定义Web容器监听器(ServletContextListener),这两者任何一个都可以启动Spring Web应用上下文的工作。所有版本的web容器都可以定义自启动的Servlet,但只有Servlet 2.3及以上版本的web容器才支持Web容器监听器。但也有例外:Weblogic 8.1、WebSphere 5.x、      Oracle OC4J 9.0。

Spring分别提供了用于启动WebApplicationContext的Servlet 和 Web容器监听器:

ContextLoaderServlet(org.springframework.web.context.ContextLoaderServlet):

<!-- 声明自动启动的Servlet --><servlet><servlet-name>springContextLoaderServer</servlet-name><servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class><!-- 启动顺序 --><load-on-startup>1</load-on-startup></servlet>

ContextLoaderListener(org.springframework.web.context.ContextLoaderListener):

<!-- 指定配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value> classpath*:spring/spring-context.xml, classpath*:spring/spring-database.xml </param-value></context-param><!-- 声明web容器监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

在启动 WebApplicationContext 之前,需要使用日志文件Log4J,Log4J 必须在 WebApplicationContext 之前加载

<!-- 指定配置文件 --><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:log4j.xml</param-value></context-param><!-- 声明Log4J监听器 --><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>

<!-- 装载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>

如果使用@Configuration的java类提供配置信息,则web.xml需要配置如下:

<web-app><!-- 通过指定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.taobao.Car1, com.taobao.Car2</param-value></context-param><!-- 监听启动容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>