Spring's Bean and Bean life cycle of post-processors

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43687990/article/details/102498840

1.Spring IOC container can manage the lifecycle of Bean, Spring allows customization of task execution at a specific point Bean's life cycle.

Process 2.Spring IOC Bean container life cycle of managing:
(1) Create an instance constructor or by Bean factory method
(2) to set a value of the Bean properties and other references to the Bean
initialization call Bean (3) method
(. 4) may be used Bean
(5) when the container is closed, calls the destroy method of Bean
configuration init-method and Bean destory-method attribute declaration, the method specified initialization and destruction Bean

Sample code:

//创建一个Car类
public class Car{
	private String name;
	public void setName(String name){
		this.name = name;
	}

	public void init1(){
		System.out.println("这是bean的初始化init.....");
	}
	
	public void destory(){
		System.out.println("这是Bean的销毁方法");
	}
}

xml configuration

<bean class="Car的全类名" id = "car"  p:name = "audi"  init-method="init1" destory-method="destory"></bean>

Bean post-processors

1.Bean postprocessor Bean allow additional processing before and after the initialization method is called. Bean post processor processing one by one for all the Bean instance IOC container, rather than a single instance.
Typical applications: check the correctness Bean Bean property or change the properties according to specific criteria

2. Bean for post-processor, implement BeanPostProcessor interface, before and after the initialization method is invoked, Spring Bean will each instance are transmitted to the above-described two interface methods:

//Bean初始化之前
postProcessBeforeInitialization(Object bean, String beanName)  

//Bean初始化之后
postProcessAfterInitialization(Object bean, String beanName)

After addition lifecycle Bean Bean postprocessor
Spring IOC container of Bean lifecycle management process:
creating by Bean instance constructor or factory method
to set the value Bean Bean properties and other references
passed to Bean instance method postProcessBeforeInitialization Bean postprocessor
calls the initialization method Bean
postProcessAfterInitialization method passes Bean to Bean instance postprocessor
Bean can be used
when the container is closed, calls the destroy method of Bean

Sample code (a case based on the above code):

public class CarPostProcessor implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("afterinit..." +bean+beanName);
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("bean init :"+bean+beanName);
		return bean;
	}

}

xml configuration:

<bean class="CarPostProcessor的全类名"></bean>

Guess you like

Origin blog.csdn.net/qq_43687990/article/details/102498840