Recently, during the development process, there is a problem with the load order of Spring beans. When the container is started, the two-level cache will be loaded through the InitializingBean's afterPropertiesSet. However, during the loading process, the load container object is not loaded into the Spring container, so today we will consolidate the load order of spring beans and will be affected by those.
In the default configuration, Spring's bean loading order is not random, but it is not strictly in a predetermined order. By default, Spring loads and initializes beans according to the following policies:
1. Determinism of loading order
- Configuration order: Beans defined in Spring XML configuration files or Java configuration classes are usually loaded in the defined order.
-
Annotation scan order: When scanning using component (e.g.
@ComponentScan
) When Spring loads the beans in the order of classpath scanning. But this order may vary in different operating environments, because the order of classpath scanning depends on the file system or JAR packages arranged.
2. Analysis of dependencies
Spring will parse the dependencies between beans when loading and initializing beans, ensuring that the dependent beans are loaded and initialized first. This means that if Bean A depends on Bean B, then Bean B will load and initialize before Bean A.
3. Bean initialization order
-
@DependsOn
annotation: Can be used@DependsOn
Annotation explicitly specifies that one bean depends on another bean or more. In this way, the dependent bean will be initialized first. -
depends-on
property: In XML configuration, you can usedepends-on
The attribute explicitly specifies that one bean depends on another bean or more.
4. FactoryBean
FactoryBean
Initialization will be given priority over ordinary beans, as they are responsible for creating instances of other beans.
5. Lifecycle callback method
Spring ensures that the beans perform lifetime callbacks in the following order:
-
BeanPostProcessor
ofpostProcessBeforeInitialization
method - Initialize the callback (e.g.
InitializingBean
ofafterPropertiesSet
Method or customizationinit-method
) -
BeanPostProcessor
ofpostProcessAfterInitialization
method
Sample code and description
Here are some sample code that shows the order of bean loading in different situations:
XML configuration
<bean class=""/> <bean class="" depends-on="beanA"/>
in this case,beanA
Will be beforebeanB
Load and initialize.
Annotation configuration
@Configuration @ComponentScan(basePackages = "") public class AppConfig { @Bean @DependsOn("beanA") public BeanB beanB() { return new BeanB(); } @Bean public BeanA beanA() { return new BeanA(); } }
In this configuration,beanA
Will be beforebeanB
Loading and initializing becausebeanB
Used@DependsOn
annotation.
Example of code to check the order of beans loading
To demonstrate the order in which Spring Beans load, we can write a simple Spring application and print the log in the bean's constructor:
@Component public class BeanA { public BeanA() { ("BeanA instantiated"); } } @Component public class BeanB { public BeanB() { ("BeanB instantiated"); } } @Configuration @ComponentScan(basePackages = "") public class AppConfig { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(); // Force initialization of beans (); (); } }
In this example, if you run the program, the order in which the console output will indicate the order in which the beans are loaded. This method can help us verify the loading order of beans in different configurations and environments.
Summarize
Under the default configuration, Spring's bean loading order mainly depends on the bean's definition order, dependencies and lifecycle callback methods. Although the order of classpath scanning may vary by operating environment, it is done by using@DependsOn
Annotations anddepends-on
Properties that can explicitly control the loading and initialization order of the bean. Spring does not randomly change the loading order of beans, but follows the above strategy to ensure that beans are loaded and initialized in a reasonable order.
This is the end of this article about a deep understanding of the loading order of Spring beans. For more related content on Spring beans, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!