Content of this article
- Do not use the initialization and destruction order of dependencies on beans
- Initialization and destruction order of bean s after using dependencies on
- Conclusions and application scenarios of dependencies on
Conclusion first
Initialization: the dependencies on attribute can explicitly force one or more beans to be initialized before the beans that use this element are initialized.
Destroy: those dependent beans specified by the dependencies on attribute are destroyed first, and the beans using this element are destroyed later, which interferes with the closing order.
After the conclusion is given, we will verify it through a case.
Do not use the initialization and destruction order of dependencies on beans
Three simple classes are defined, including init and destroy methods, which are used to observe the order
public class MyBeanA { public void init() { System.out.println("MyBeanA Initialized-----"); } public void destroy() { System.out.println("MyBeanA Destroyed-----"); } } public class MyBeanB { public void init() { System.out.println("MyBeanB Initialized-----"); } public void destroy() { System.out.println("MyBeanB Destroyed-----"); } } public class MyBeanC { public void init() { System.out.println("MyBeanC Initialized-----"); } public void destroy() { System.out.println("MyBeanC Destroyed-----"); } }
The bean in the configuration defines and specifies the initialization method and destruction method
<?xml version="1.0" encoding="UTF-8"?> <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"> <bean class="com.crab.spring.ioc.demo04.MyBeanA" init-method="init" destroy-method="destroy" id="myBeanA"/> <bean class="com.crab.spring.ioc.demo04.MyBeanB" init-method="init" destroy-method="destroy" id="myBeanB"/> <bean class="com.crab.spring.ioc.demo04.MyBeanC" init-method="init" destroy-method="destroy" id="myBeanC"/> </beans>
Test the initialization and destruction of bean s by the container
package com.crab.spring.ioc.demo04; /** * @author zfd * @version v1.0 * @date 2022/1/13 15:11 * @For me, please pay attention to the official account of crab Java notes for more technical series. */ public class Test { @org.junit.Test public void test() { System.out.println("Start creating containers and initializing bean"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("demo04/spring1.xml"); System.out.println("Start closing containers and destroying bean"); context.close(); } }
Conclusion at this stage
Output results
Start creating containers and initializing bean MyBeanA Initialized----- MyBeanB Initialized----- MyBeanC Initialized----- Start closing containers and destroying bean MyBeanC Destroyed----- MyBeanB Destroyed----- MyBeanA Destroyed-----
The initialization order is the configuration order of bean s in the configuration file A-B-C
The destruction sequence is opposite to the initialization sequence C-B-A
Initialization and destruction order of bean s after using dependencies on
On the basis of the above case, adjust the configuration of MyBeanA
<?xml version="1.0" encoding="UTF-8"?> <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"> <bean class="com.crab.spring.ioc.demo04.MyBeanA" init-method="init" destroy-method="destroy" id="myBeanA" depends-on="myBeanC"/> <bean class="com.crab.spring.ioc.demo04.MyBeanB" init-method="init" destroy-method="destroy" id="myBeanB"/> <bean class="com.crab.spring.ioc.demo04.MyBeanC" init-method="init" destroy-method="destroy" id="myBeanC"/> </beans>
Like the test program, specify the configuration file
@org.junit.Test public void test2() { System.out.println("Start creating containers and initializing bean"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("demo04/spring2.xml"); System.out.println("Start closing containers and destroying bean"); context.close(); }
Conclusion at this stage
Operation results
Start creating containers and initializing bean MyBeanC Initialized----- MyBeanA Initialized----- MyBeanB Initialized----- Start closing containers and destroying bean MyBeanB Destroyed----- MyBeanA Destroyed----- MyBeanC Destroyed-----
Initialization sequence: C-A-B. in different places, A depends on C, so C initializes first, then to A, and then to B
Order of destruction: B-A-C, destroy A and then destroy C
Conclusions and application scenarios of dependencies on
Intervention initialization: for example, when it is necessary to trigger the static initialization method in the class to register the database driver, it depends on the configuration bean of the database to initialize first.
Intervention in destruction: for example, in our cleaning class A, some data in the process is saved to redis in the destruction method, which depends on RedisTemplate, so RedisTemplate must be destroyed after a.
summary
This paper introduces the impact of dependencies on bean initialization and destruction and the actual application scenarios. The next article introduces automatic dependency injection.
Source code address: https://github.com/kongxubihai/pdf-spring-series/tree/main/spring-series-ioc/src/main/java/com/crab/spring/ioc/demo04
Knowledge sharing, please indicate the source of reprint. There is no order in learning, and the one who reaches is the first!