本文主要是介绍(九)Spring教程——ApplicationContext中Bean的生命周期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.前言
ApplicationContext中Bean的生命周期和BeanFactory中的生命周期类似,不同的是,如果Bean实现了org.springframework.context.ApplicationContextAware接口,则会增加一个调用该接口方法setApplicationContext()的步骤。
此外,如果在配置文件中声明了工厂后处理器接口BeanFacotryPostProcessor的实现类,则应用上下文在装载配置文件后、初始化Bean实例之前将调用这些BeanFactoryPostProcessor对配置信息进行加工处理。Spring框架提供了多个工厂后处理器,如CustomEditorConfigurer、PopertyPlaceholderConfigurer等。如果在配置文件中定义了多个工厂后处理器,那么最好让它们实现org.springframework.core.Ordered接口,以便Spring以确定的顺序调用它们。工厂后处理器是容器级的,仅在应用上下文初始化时调用依次,其目的是完成一些配置文件的加工处理工作。
ApplicationContext和BeanFactory另一个最大的不同之处在于:前者会利用Java反射机制自动识别出配置文件中定义的BeanPostProcessor、InstantiationAwareBeanPostProcessor和BeanFactoryPostProcessor,并自动将它们注册到应用上下文中;而后者需要在代码中通过手工调用addBeanPostProcessor()方法进行注册。这也是为什么在应用开发时普遍使用ApplicationContext而很少使用BeanFactory的原因之一。
在ApplicationContext中,只需要在配置文件中通过<bean>定义工厂后处理器和Bean后处理器,它们就会按预期的方式运行。
2.创建Car类
首先,我们对上一篇文章中的Car类稍微修改以下,修改后的代码如下:
package com.example.servlet001.bean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.*;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;// 管理Bean生命周期的接口public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, ApplicationContextAware {private String brand;private String color;private int maxSpeed;private BeanFactory beanFactory;private String beanName;public Car(){System.out.println("调用Car()构造函数");}//InitializingBean接口方法@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("调用InitializingBean.afterPropertiesSet()。");}//BeanNameAware接口方法@Overridepublic void setBeanName(String s) {System.out.println("调用BeanName.setBeanName().");this.beanName=s;}//BeanFactoryAware接口方法@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("调用BeanFactoryAware.setBeanFactory()");this.beanFactory=beanFactory;}//DisposableBean接口方法@Overridepublic void destroy() throws Exception {System.out.println("调用DisposableBean.destroy()。");}//通过<bean>的init-method属性指定的初始化方法public void myInit(){System.out.println("调用init-method所指定的myInit(),将maxSpeed设置为240。");this.maxSpeed=240;}//通过<bean>的destroy-method属性指定的销毁方法public void myDestroy(){System.out.println("调用destroy-method所指定的myDestroy()。");}public String getBrand(){return brand;}public void setBrand(String brand){System.out.println("调用setBrand()设置属性。");this.brand=brand;}public String getColor(){return color;}public void setColor(String color){this.color=color;}public int getMaxSpeed(){return maxSpeed;}public void setMaxSpeed(int maxSpeed){this.maxSpeed=maxSpeed;}public void introduce(){System.out.println("brand:"+brand+";color:"+color+";maxSpeed:"+maxSpeed);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {System.out.println("这是ApplicationContextAware的setApplicationContext()方法");}}
3.定义工厂后处理器
定义一个工厂后处理器,MyBeanFactoryPostProcessor.java,该类的代码如下:
package com.example.servlet001;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;/*** 工厂后处理器*/public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {//对car的brand属性配置信息进行“偷梁换柱”的加工操作@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {BeanDefinition bd=bf.getBeanDefinition("car1");bd.getPropertyValues().addPropertyValue("brand","奇瑞QQ");System.out.println("调用BeanFactoryPostProcessor.postProcessBeanFactory()");}}
ApplicationContext在启动时,将首先为配置文件中的每个<bean>生成一个BeanDefinition对象,BeanDefinition是<bean>在Spring容器中的内部表示。当配置文件中所有的<bean>都被解析成BeanDefinition时,ApplicationContext将调用工厂后处理器的方法,因此,有机会通过程序的方式调整Bean的配置信息。在该后处理器中,将car1对应的BeanDefinition进行调整,将brand属性设置为“奇瑞QQ” 。
4.BeanPostProcessror实现类
此外,还提供了一个BeanPostProcessror实现类,在该类中仅对car Bean进行处理,对配置文件所提供的属性设置值进行判断,并执行相应的“查漏补缺”操作,MyBeanPostProcessor.java的代码如下图所示:
package com.example.servlet001;import com.example.servlet001.bean.Car;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object o, String s) throws BeansException {if(s.equals("car1")){Car car = (Car)o;if(car.getColor() == null){System.out.println("调用MyBeanPostProcessor.postProcessBeforeInitialization(),color为空,设置为默认黑色。");car.setColor("黑色");}}return o;}@Overridepublic Object postProcessAfterInitialization(Object o, String s) throws BeansException {if(s.equals("car1")){Car car = (Car)o;if(car.getMaxSpeed() >= 200){System.out.println("调用MyBeanPostProcessor.postProcessAfterInitialization(),将maxSpeed调整为200。");car.setMaxSpeed(200);}}return o;}}
5.容器级后处理器
MyInstantiationAwareBeanPostProcessor.java代码如下
package com.example.servlet001;import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;import java.beans.PropertyDescriptor;public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {if("car1".equals(beanName)){System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");}return null;}@Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {if("car1".equals(beanName)){System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");}return true;}@Overridepublic PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {if("car1".equals(beanName)){System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");}return pvs;}}
6.配置文件
修改resource文件夹下的test.xml配置文件,xml配置文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><!--这个brand属性的值将会被工厂处理器更改掉--><bean id="car1" name="car1" class="com.example.servlet001.bean.Car"init-method="myInit"destroy-method="myDestroy"p:brand="红旗"p:maxSpeed="200"/><!--容器级声明周期处理--><bean id="myInstantiationAwareBeanPostProcessor" class="com.example.servlet001.MyInstantiationAwareBeanPostProcessor"></bean><!--注册Bean后处理器--><bean id="myBeanPostProcessor"class="com.example.servlet001.MyBeanPostProcessor"></bean><!--工厂后处理器--><bean id="myBeanFactoryPostProcessor"class="com.example.servlet001.MyBeanFactoryPostProcessor"></bean></beans>
7.结果验证
创建一个Demo1.java类来测试该配置的运行效果,Demo1.java代码如下图所示:
package com.example.servlet001;import com.example.servlet001.bean.Car;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo1 {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("test.xml");Car car=(Car)ac.getBean("car1");car.introduce();}}
点击运行后的结果如图所示
这篇关于(九)Spring教程——ApplicationContext中Bean的生命周期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!