Container management bean Life cycle of ,bean The lifecycle of contains the creation of — initialization — Destruction and other processes .

Spring Allow us to Bean Perform specific actions after initialization and before destruction . There are basically three ways to specify specific operations .

1. initMethod() And destroyMethod()

​ First declare a basic Java class :
public class Car { private String name; public Car(String name) { this.name =
name; System.out.println("Car...Constructor "); } public void init(){ System.out
.println("car name:"+name); System.out.println("car ... init ..."); } public
void destory(){ System.out.println("car ... destory ..."); } // ellipsis get/set }
​ use XML Mode configuration Bean When , adopt Elemental init-method/destroy-method Property can specify after initialization , Operation method before destruction .
<bean id="car" class="com.baiding.model.Car" init-method="init" destroy-method=
"destory"> <property name="name" value="audi"/> </bean>
​ But in use @Bean Note statement bean When , The above can also be done XML Configure the same function , The basic examples are as follows :

Make a statement Java Configuration class , Use in configuration class @Bean Annotations declare a component
@Configuration public class LifecycleConfig { @Bean(initMethod = "init"
,destroyMethod ="destory") public Car car(){ return new Car("audi"); } }
Use the test class to test it
public class LifecycleTest { @Test void test01(){ ApplicationContext ac = new
AnnotationConfigApplicationContext(LifecycleConfig.class); System.out.println(
" Container created ..."); } }
Execute this test method . In this test method , We just created the container .
Car...Constructor car name:audi car ... init ... Container created ... car ... destory ...
@Bean Annotation creates a single instance by default bean, For single instance bean Come on :

When the object is created , After the assignment is completed , Start calling initialization method initMethod()

When the container is closed , Execution of destruction method destoryMethod()

For multi instance bean Come on , Each acquisition bean An instance is created , The container will not manage this bean, In other words, multiple instances are not called when the container is closed bean Method of destruction ( Just put the bean Of @Scope Set to prototype, You can see that the destroy method is not called when the container is closed ).

2. InitializingBean And DisposableBean

​ You can also let bean realization InitializingBean Interface Define self initialization logic , realization DisposableBean Interface definition destroy logic .

It's just started Car class : Implement the above two interfaces
public class Car implements InitializingBean,DisposableBean{ private String
name;public Car(String name) { this.name = name; System.out.println(
"Car...Constructor "); } public void initMethod(){ System.out.println("car
name:"+name); System.out.println("car ... initMothod ..."); } public void
destoryMethod(){ System.out.println("car ... destoryMethod ..."); } public void
destroy() throws Exception { System.out.println("car ...DisposableBean...
destory"); } public void afterPropertiesSet() throws Exception {
System.out.println("car...InitializingBean...afterPropertiesSet"); } //
ellipsis get,set }
Configuration class and test class remain unchanged , Look directly at the results :
Car...Constructor car...InitializingBean...afterPropertiesSet car name:audi car
... initMothod ... Container created ... car ...DisposableBean... destory car ...
destoryMethod...

As can be seen from the results , Define its own initialization or destruction logic by implementing the interface , The same is true when the container is created ,bean After the object is constructed and the assignment is successful , Start execution of initialization method , Execute the destroy logic after the container is closed .

3. @PostConstruct And @PreDestroy

​ Can be added to the specified method @PostConstruct perhaps @PreDestroy annotation , To specify that the method is in the bean Call after initialization or before destruction .

​ It's just started Car class : Add two methods , Add two notes respectively .
public class Car implements InitializingBean,DisposableBean{ private String
name;public Car(String name) { this.name = name; System.out.println(
"Car...Constructor "); } public void initMethod(){ System.out.println("car
name:"+name); System.out.println("car ... initMothod ..."); } public void
destoryMethod(){ System.out.println("car ... destoryMethod ..."); } public void
destroy() throws Exception { System.out.println("car ...DisposableBean...
destory"); } public void afterPropertiesSet() throws Exception {
System.out.println("car...InitializingBean...afterPropertiesSet"); }
@PostConstruct public void postConstruct(){ System.out.println(
"car...postConstruct"); } @PreDestroy public void preDestroy(){
System.out.println("car... preDestroy"); } // ellipsis get,set }
Configuration class and test class remain unchanged , Look directly at the results :
Car...Constructor car...postConstruct
car...InitializingBean...afterPropertiesSet car name:audi car... initMothod ...
Container created ... car... preDestroy car ...DisposableBean... destory car ...
destoryMethod...
It can be seen that ,@PostConstruct And @PreDestroy The functions of these two annotations are basically similar to those of the above two methods , Only when they work at the same time , There are some rules in the execution sequence .

Execute after initialization : @PostConstruct > InitializingBean > init-method

Before destruction :@preDestroy > DisposableBean > destoryMethod

Technology
©2019-2020 Toolsou All rights reserved,
Hundreds of millions of locusts rarely collide Locusts want to be self driving Heroes Share has surpassed Ningde Era !LG Chemical confirmation to spin off battery business unit TypeScript Data types in is enough Python Garbage collection and memory leak msf Generate Trojan horse attack android mobile phone Element-UI Implementation of secondary packaging TreeSelect Tree drop-down selection component element-ui+vue-treeselect Verification of drop down box Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class A guess number of small games , use JavaScript realization Unity3D Input Key system