1. The Bean’s life

It can be summarized as the following 9

  • Instantiate the Bean object (through the Bean factory or constructor)
  • Setting object properties (Setter method injection)
  • If the Bean implements BeanNameAware, the factory calls the Bean’s setBeanName () method to pass the Bean’s ID. (check aware interface)
  • If the Bean implements BeanNameAware, the factory calls the setBeanFactory () method to pass in the factory itself
  • Pass the Bean instance to the Bean’s front processor postProcessorBeforeInitialization (Object beans, String beanName) method
  • Call the bean’s initialization method
  • Pass the Bean instance to Bean postProcessAfterInitialization post processor (Object Bean, String beanName) method
  • Using Bean
  • Before container destruction, the bean’s destruction method is called

1.1 Simple demo with no rear processor


import org.springframework.beans.factory.BeanNameAware;

/ * * *@Description: a student class (Bean) that represents its life cycle */
public class Student implements BeanNameAware {
	private String name;

	// no argument constructor
	public Student(a) {
		super(a); }/** Sets object properties *@param name the name to set
	 */
	public void setName(String name) {
		System.out.println("Set object property setName()..");
		this.name = name;
	}
	
	// The Bean's initialization method
	public void initStudent(a) {
		System.out.println("Student this Bean: initialize");
	}
	
	// The Bean destruction method
	public void destroyStudent(a) {
		System.out.println("Student this Bean: Destroy");
	}
	
	/ / using the beans
	public void play(a) {
		System.out.println("Student this Bean: use");
	}

	/* toString * @see java.lang.Object#toString() */
	@Override
	public String toString(a) {
		return "Student [name = " + name + "]";
	}

	// Call BeanNameAware setBeanName()
	// Pass the ID of the Bean.
	@Override
	public void setBeanName(String name) {
		System.out.println("Call BeanNameAware setBeanName()..."); }}Copy the code
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CycleTest {
    @Test
 	public void test(a) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student student = (Student) context.getBean("student");
		/ / using the beans
		student.play();
		System.out.println(student);
		// Close the container((AbstractApplicationContext) context).close(); }}Copy the code

      
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
   <! -- init-method: specifies the initialization method destroy-method: specifies the destruction method -->
		<bean id="student" class="Student" init-method="initStudent" destroy-method="destroyStudent">
		</bean>
</beans>
Copy the code

It can be seen that the life of the bean completely matches the previous process diagram.

  • Init-method: specifies the initialization method
  • Destroy-method: specifies the destroy method

Speaking of init-method and destroy-method, it’s also worth mentioning the properties in < beans>

  • Default-init-method: Sets a common initialization method for all beans in the application context
  • Default-destroy-method: Sets a common destruction method for all beans in the application context

2.Bean post-processor

The life of the bean above is actually a very complete explanation of the bean life cycle, but the bean post-processor is an enhancement to the bean

2.1 usage

Provide your own instantiation logic to the Bean object before and after the Bean’s initialization

- implementation - postProcessBeforeInitialization method - postProcessAfterInitialization BeanPostProcessor interfaceCopy the code

Student.java


import org.springframework.beans.factory.BeanNameAware;

public class Student implements BeanNameAware {
	private String name;

	// no argument constructor
	public Student(a) {
		super(a); }/** Sets object properties *@param name the name to set
	 */
	public void setName(String name) {
		System.out.println("Set object property setName()..");
		this.name = name;
	}
	
	// The Bean's initialization method
	public void initStudent(a) {
		System.out.println("Student this Bean: initialize");
	}
	
	// The Bean destruction method
	public void destroyStudent(a) {
		System.out.println("Student this Bean: Destroy");
	}
	
	/ / using the beans
	public void play(a) {
		System.out.println("Student this Bean: use");
	}

	/* toString * @see java.lang.Object#toString() */
	@Override
	public String toString(a) {
		return "Student [name = " + name + "]";
	}

	// Call BeanNameAware setBeanName()
	// Pass the ID of the Bean.
	@Override
	public void setBeanName(String name) {
		System.out.println("Call BeanNameAware setBeanName()..."); }}Copy the code

Mybeanpostprocessor.java (implement BeanPostProcessor interface)


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

/ * * * the post processor of bean * respectively before and after the bean initialization of bean object provides its own * postProcessAfterInitialization instantiation logic: After initialization to enhance the bean * postProcessBeforeInitialization: to enhance the beans before initialization * * /
public class MyBeanPostProcessor implements BeanPostProcessor {

	// Process the Bean after initialization
	// Parameter: bean: the bean to be initialized
	// Parameter: beanname: the beanname
	// Return value: the bean returned to the user, which can be modified or returned as a new bean
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanname) throws BeansException {
		Student stu = null;
		System.out.println("Processing the Bean after initialization, changing the value of the Bean's member variable.");
		if("name".equals(beanname) || bean instanceof Student) {
			stu = (Student) bean;
			stu.setName("Jack");
		}
		return stu;
	}

	// Process the Bean before initialization
	// Parameter: bean: the bean to be initialized
	// Parameter: beanname: the beanname
	// Return value: the bean returned to the user, which can be modified or returned as a new bean
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanname) throws BeansException {
		System.out.println("Process the Bean before initialization, at this point my name"+bean);
		returnbean; }}Copy the code

TEST

package com.linjie.cycle;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CycleTest {
    @Test
 	public void test(a) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student student = (Student) context.getBean("student");
		/ / using the beans
		student.play();
		System.out.println(student);
		// Close the container((AbstractApplicationContext) context).close(); }}Copy the code

      
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <! -- init-method: specifies the initialization method destroy-method: specifies the destruction method -->
		<bean id="student" class="com.linjie.cycle.Student" init-method="initStudent" destroy-method="destroyStudent">
		<property name="name" value="fq"></property>
		</bean>
		
		<! The IoC container automatically recognizes that the bean is a BeanPostProcessor.
		<bean class="com.fq.cycle.MyBeanPostProcessor"></bean> 
</beans>
Copy the code

The results of

You can see in applicationContext.xml that the configuration Bean post-processor does not need an ID, just its full class name, because the IoC container automatically recognizes a BeanPostProcessor

In the console display results can be seen that the Bean’s rear power of the processor, Bean implementation can be what you want to do, such as my Demo here is in the heart of the postProcessAfterInitialization method member variable name secretly changed, the final output is the value of modified