---恢复内容开始---
Spring中关于Bean的生命周期,我总结的大致过程如下:
1、用户调用BeanFactory接口的getBean()方法请求一个Bean时,如果容器注册了InstantiationAwareBeanPostProcessor接口则调用postProcessorBeforeInstantiation()方法,这个过程在实例化Bean之前进行。
2、调用该请求Bean的构造函数进行实例化。
3、如果容器注册了InstantiationAwareBeanPostProcessor接口则调用postProcessorAfterInstantiation()方法,这个过程在实例化Bean之后进行。
4、如果配置信息里有<property>调用InstantiationAwareBeanPostProcessor接口下的PostPropertyValues()方法。
5、调用该请求Bean的set方法设置属性值。
6、如果该Bean实现了BeanNameAware接口则调用setBeanName()方法,设置bean名称。
7、如果该Bean实现了BeanFactoryAware接口则调用setBeanFactory()方法,设置BeanFactory容器。
8、如果容器注册了BeanPostProcessor后处理器接口则调用 Object postProcessorBeforeInitialization(Object bean,String beanName)方法,其中bean为当前正在处理的bean,beanName为当前Bean的配置名,返回对象为处理后的bean。
9、如果该 bean实现了InitializingBean接口则调用afterPropertiesSet()方法。
10、如果在<bean init-method="myInit">中定义了init-method属性则执行该方法。
11、如果容器注册了BeanPostProcessor后处理器接口则调用Object postProcessorAfterInitialization(Object bean,String beanName)。
12、如果指定作用域<bean scope="prototype">则将bean返回给调用者,调用者负责该bean的后续管理,如果指定作用域<bean scope="singleton">则将bean放入IOC缓冲池,并将bean返回给调用者,由Spring容器继续管理该bean。
Spring中Bean生命周期的实例:
1、新建一个maven项目,pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smart</groupId>
<artifactId>chapter3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、结构如下红色标记内容:
3、Car.java类:
package com.smart.beanfactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Car implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
private String brand;
private String color;
private int maxSpeed;
private BeanFactory beanFactory;
private String beanName;
public Car() {
// TODO Auto-generated constructor stub
System.out.println("调用Car()构造函数....");
}
public void setBrand(String brand) {
System.out.println("调用setBrand()方法设置属性....");
this.brand = brand;
}
public void introduce() {
System.out.println("brand:"+brand+",color:"+color+",maxSpeed:"+maxSpeed);
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
System.out.println("调用setMaxSpeed()方法设置属性....");
this.maxSpeed = maxSpeed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
System.out.println("调用setColor()方法设置属性....");
this.color = color;
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("调用DisposableBean下的destroy()方法....");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("调用InitializingBean下的afterPropertiesSet()方法....");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("调用BeanFactoryAware下的setBeanFactory()方法....");
this.beanFactory = beanFactory;
}
@Override
public void setBeanName(String beanName) {
// TODO Auto-generated method stub
System.out.println("调用BeanNameAware下的setBeanName()方法....");
this.beanName = beanName;
}
public void myInit() {
System.out.println("调用<bean init-method='myInit'>方法,将maxSpeed设置为240....");
this.maxSpeed = 240;
}
public void myDestroy() {
System.out.println("调用<bean destroy-method='myDestroy'>....");
}
}
4、MyInstantiationAwareBeanPostProcessor.java类:
package com.smart.beanfactory;
import java.beans.PropertyDescriptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
if(beanName.equals("car")) {
Car car = (Car)bean;
if(car.getMaxSpeed() >= 200) {
System.out.println("调用InstantiationAwareBeanPostProcessorAdapter中的postProcessAfterInitialization()方法...");
car.setMaxSpeed(200);
}
}
return bean;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
if(beanName.equals("car")) {
System.out.println("在实例化Bean之后调用postProcessorAfterInstantiation()方法....");
}
return true;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
if(beanName.equals("car")) {
Car car= (Car)bean;
if(car.getColor() == null) {
System.out.println("调用InstantiationAwareBeanPostProcessorAdapter中的postProcessorInitialization()方法....");
car.setColor("黑色");
}
}
return bean;
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
// TODO Auto-generated method stub
if(beanName.equals("car")) {
System.out.println("实例化bean之前调用postProcessorBeforeInstantiation()方法...");
}
return null;
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
String beanName) throws BeansException {
// TODO Auto-generated method stub
if(beanName.equals("car")) {
System.out.println("如果配置信息里设置了属性则调用postPropertyValues()方法....");
}
return pvs;
}
}
5、BeanLifeCycle.java类:
package com.smart.beanfactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class BeanLifeCycle {
private static void lifeCycleBeanFactory() {
Resource res = new ClassPathResource("bean.xml");
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(res);
beanFactory.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
Car car1 = (Car) beanFactory.getBean("car");
car1.introduce();
car1.setColor("红色");
Car car2 = (Car) beanFactory.getBean("car");
System.out.println("car1==car2:"+(car1==car2));
beanFactory.destroySingletons();
}
public static void main(String[] args) {
lifeCycleBeanFactory();
}
}
6、bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="car" class="com.smart.beanfactory.Car" init-method="myInit" destroy-method="myDestroy">
<property name="brand" value="红旗CA72"/>
<property name="maxSpeed" value="200"/>
</bean>
</beans>
7、运行结果: