Spring Bean life cycle analysis

First, the life cycle flow chart

Spring Bean's complete lifecycle from start to create a Spring container, until the final destruction of the Spring container Bean, which contains a series of key points.

Spring Bean life cycle analysis


Spring Bean life cycle analysis


Second, a variety of interface methods classification

Bean's complete life cycle undergone various method calls, these methods can be divided into the following categories:

1, Bean own method: This method comprises a method call, and the Bean itself specified by the configuration file <bean> the init-method and destroy-method

2, Bean stage life cycle interface method: This method includes BeanNameAware, BeanFactoryAware, InitializingBean and DiposableBean these interfaces

3, container-level lifecycle interface method: this includes both InstantiationAwareBeanPostProcessor and BeanPostProcessor interface, the general said that their implementation classes for the "post-processor."

4, the processor interface factory method: This includes AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer the like method after the processor interface useful plants. After the processor is factory-container. Call immediately after application context assembled configuration file.

Third, demonstration

We use a simple Spring Bean to show you Spring Bean's life cycle.

1. The first is a simple Bean of the Spring , calling their methods and Bean Bean class lifecycle interface methods, in order to facilitate the presentation, which implements BeanNameAware , the BeanFactoryAware , InitializingBean and DiposableBean four interfaces, while there are two methods, the corresponding configuration file <bean> the init-method and destroy-method. as follows:

package com.study.vo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
 private String name;
 private String address;
 private int phone;
 private BeanFactory beanFactory;
 private String beanName;
 public Person() {
 System.out.println ( "[] constructor calls the constructor instantiated Person");
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 System.out.println ( "[] injection properties injected property name");
 this.name = name;
 }
 public String getAddress() {
 return address;
 }
 public void setAddress(String address) {
 System.out.println ( "[] injection properties injected property address");
 this.address = address;
 }
 public int getPhone() {
 return phone;
 }
 public void setPhone(int phone) {
 System.out.println ( "[] injection properties injected property phone");
 this.phone = phone;
 }
 @Override
 public String toString() {
 return "Person [address=" + address + ", name=" + name + ", phone=" + phone + "]";
 }
 // This is BeanFactoryAware interface method
 @Override
 public void setBeanFactory(BeanFactory arg0) throws BeansException {
 System.out.println ( "[the interface] calls BeanFactoryAware BeanFactoryAware.setBeanFactory ()");
 this.beanFactory = arg0;
 }
 // This is BeanNameAware interface method
 @Override
 public void setBeanName(String arg0) {
 System.out.println ( "[the interface] calls BeanNameAware BeanNameAware.setBeanName ()");
 this.beanName = arg0;
 }
 // This is the interface method InitializingBean
 @Override
 public void afterPropertiesSet() throws Exception {
 System.out.println ( "[the interface] calls InitializingBean InitializingBean.afterPropertiesSet ()");
 }
 // This is DiposibleBean interface method
 @Override
 public void destroy() throws Exception {
 System.out.println ( "[the interface] calls DiposibleBean DiposibleBean.destory ()");
 }
 init-method // attribute specified by the initializer
 public void myInit() {
 System.out.println ( "initialization method init-method property specifies] [calls init-method");
 }
 destroy-method // attribute specified by the initializer
 public void myDestory() {
 System.out.println ( "initialization method destroy-method [] destroy-method property specifies a call");
 }
}

2. Next is a demonstration BeanPostProcessor interface method , as follows:

package com.study.vo;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

public MyBeanPostProcessor() {

super();

System.out.println("这是BeanPostProcessor实现类构造器!!");

// TODO Auto-generated constructor stub

}

@Override

public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {

System.out.println("【BeanPostProcessor接口】调用方法postProcessAfterInitialization对属性进行更改!");

return arg0;

}

@Override

public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {

System.out.println("【BeanPostProcessor接口】调用方法postProcessBeforeInitialization对属性进行更改!");

return arg0;

}

}

如上,BeanPostProcessor接口包括2个方法postProcessAfterInitialization和postProcessBeforeInitialization,这两个方法的第一个参数都是要处理的Bean对象,第二个参数都是Bean的name。返回值也都是要处理的Bean对象。这里要注意。

3. InstantiationAwareBeanPostProcessor 接口本质是BeanPostProcessor的子接口,一般我们继承Spring为其提供的适配器类InstantiationAwareBeanPostProcessor Adapter来使用它,如下:

package com.study.vo;

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 {

public MyInstantiationAwareBeanPostProcessor() {

super();

System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!");

}

// 接口方法、实例化Bean之前调用

@Override

public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {

System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");

return null;

}

// 接口方法、实例化Bean之后调用

@Override

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");

return bean;

}

// 接口方法、设置某个属性时调用

@Override

public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");

return pvs;

}

}

这个有3个方法,其中第二个方法postProcessAfterInitialization就是重写了BeanPostProcessor的方法。第三个方法postProcessPropertyValues用来操作属性,返回值也应该是PropertyValues对象。

4. 演示工厂后处理器接口方法,如下:

package com.study.vo;
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 {
 public MyBeanFactoryPostProcessor() {
 super();
 System.out.println("这是BeanFactoryPostProcessor实现类构造器!!");
 }
 @Override
 public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
 System.out.println("【BeanFactoryPostProcessor接口】调用postProcessBeanFactory方法");
 BeanDefinition bd = arg0.getBeanDefinition("person");
 bd.getPropertyValues().addPropertyValue("phone", "110");
 }
}

5、配置文件如下applicationContext.xml,很简单,使用ApplicationContext,处理器不用手动注册:

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="beanPostProcessor" class="com.study.vo.MyBeanPostProcessor">

</bean>

<bean id="instantiationAwareBeanPostProcessor" class="com.study.vo.MyInstantiationAwareBeanPostProcessor">

</bean>

<bean id="beanFactoryPostProcessor" class="com.study.vo.MyBeanFactoryPostProcessor">

</bean>

<bean id="person" class="com.study.vo.Person" init-method="myInit"

destroy-method="myDestory" scope="singleton" p:name="张三" p:address="广州"

p:phone="159000000" />

</beans>

6. Write a test class

package com.study;

import com.study.vo.Person;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

System.out.println ( "Now start initialization container");

ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");

System.out.println ( "container initialization success");

// get Preson, and use

Person person = factory.getBean("person", Person.class);

System.out.println(person);

System.out.println ( "now closed container!");

((ClassPathXmlApplicationContext) factory).registerShutdownHook();

}

}

operation result

Spring Bean life cycle analysis


IV Summary

Analysis of operating results that may initiate the process of spring, can be more profound understanding of the life cycle of spring bean through this demo compilation.


Guess you like

Origin blog.51cto.com/14378044/2408184