Spring annotation driven development -2 Spring Bean life cycle

preface

The life cycle of a bean refers to the process of bean creation, initialization and destruction. This article explains several ways to set the initialization method and destruction method for a bean. If you want to understand the life cycle of a bean, you need to look at the original code and learn the video interrupt debugging to enhance your understanding. Article course link: Shang Silicon Valley spring annotation driven tutorial (Raytheon)

Several ways to set initialization and destruction methods for beans

1. The @ bean annotation specifies the initialization and destruction methods

example:

First, create a Car class to provide construction methods, initialization and destruction methods

public class Car {
	public Car() {
		System.out.println("...constructor...");
	}
	public void init() {
		System.out.println("... init...");
	}
	public void destroy() {
		System.out.println("... destroy...");
	}	
}

xml configuration

<bean id="person" class="com.zx.bean.Person" init-method="init" destroy-method="destroy">
    <property name="age" value="22"></property>
    <property name="name" value="zx"></property>
</bean>

@Bean annotation configuration method

@Configuration
public class MainConfig{
	@Bean(initMethod="init", destroyMethod="destroy")
	public Car car() {
		return new Car();
	}
}

The experimental process is not shown. Please see the article for details Spring annotation driven development Lesson 12···
Summary:
1. Single instance: when the container is initialized, load the bean and call the construction method and the specified initialization method; When the container is closed, its specified destruction method is called
2. Multiple instances: the container does not load beans during initialization, but only initializes when calling beans, and calls the construction method and the specified initialization method; When the container is closed, it will not be destroyed. Each time you get a bean, the IOC container will help you create an object and give it to you. When to destroy it is your own business, and the Spring container will not manage it.

2. Implement InitializingBean and DisposableBean

example

@Component
public class Cat implements InitializingBean, DisposableBean {
	public Cat() {
		System.out.println("cat...constructor...");
	}
	/**
	 * Container close call
	 */
	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...destroy...");
	}

	/**
	 * Container initialization call
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("cat...afterPropertiesSet...");
	}

3. Use the annotation defined in JSR250 specification

Contains two annotations, namely
@PostConstruct: the initialization method is executed after the bean is created and the attribute assignment is completed
@PreDestroy: notify us to clean up before the container destroys the bean s

example

@Component
public class Dog {
	public Dog() {
		System.out.println("dog...constructor...");
	}
	// After the object creation is completed and the attribute assignment is completed, it is called.
	@PostConstruct
	public void init() {
		System.out.println("dog...@PostConstruct...");
	}
	// Before the container destroys the object
	@PreDestroy
	public void destory() {
		System.out.println("dog...@PreDestroy...");
	}	
}

4. Implement the BeanPostProcessor interface

Implement two methods
postProcessBeforeInitialization: works before initialization
postProcessAfterInitialization: works after initialization

example

@Component // Add container
public class MyBeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization..." + beanName + "=>" + bean);
		return bean;
	}
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization..." + beanName + "=>" + bean);
		return bean;
	}
}

Summary: after adding MyBeanPostProcessor to the container, the component will call postProcessBeforeInitialization before initialization, and postProcessAfterInitialization after initialization. The experimental process is not shown, and the final results are as follows:


The bottom layer of spring uses a lot of BeanPostProcessor. Interested students can study it by themselves!

end...

If the summary is OK, just like it@_@ If there is any mistake, please give me some advice. The next one Spring annotation driven development -3 Spring bean attribute assignmentยทยทยท

Tags: Java Spring

Posted on Fri, 29 Oct 2021 23:10:49 -0400 by Phasma Felis