Introduction to system initializer
The principle of
- The name of the class ApplicationContextInitializer
- Describes the callback functions that are executed before the Spring container is refreshed
- Register properties with the SpringBoot container
- Inherit interface custom implementation
implementation
Methods a
1. Create FirstInitializer class implements ApplicationContextInitializer interface
@Order(1)
public class FirstInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
Map<String, Object> map = new HashMap<>();
map.put("key1"."value1");
MapPropertySource mapPropertySource = new MapPropertySource("firstInitializer", map);
environment.getPropertySources().addLast(mapPropertySource);
System.out.println("run firstInitializer"); }}Copy the code
2. Create a meta-INF /spring.factories file under resource and configure the properties
org.springframework.context.ApplicationContextInitializer=com.example.demo.initializer.FirstInitializer
Copy the code
Method 2
- Create SecondInitializer class implements ApplicationContextInitializer interface
@Order(2)
public class SecondInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
Map<String, Object> map = new HashMap<>();
map.put("key2"."value2");
MapPropertySource mapPropertySource = new MapPropertySource("secondInitializer", map);
environment.getPropertySources().addLast(mapPropertySource);
System.out.println("run secondInitializer"); }}Copy the code
- In the startup class, add the initialization example manually
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(DemoApplication.class);
application.addInitializers(newSecondInitializer()); application.run(args); }}Copy the code
Methods three
- Create ThirdInitializer class implements ApplicationContextInitializer interface
@Order(3)
public class ThirdInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
Map<String, Object> map = new HashMap<>();
map.put("key3"."value3");
MapPropertySource mapPropertySource = new MapPropertySource("thirdInitializer", map);
environment.getPropertySources().addLast(mapPropertySource);
System.out.println("run thirdInitializer"); }}Copy the code
- Add the configuration to application.properties under Resource
context.initializer.classes=com.example.demo.initializer.ThirdInitializer
Copy the code
Tips: application. The properties in the configuration, is DelegatingApplicationContextInitializer implement initialization configuration method, read the order = 0, so the first perform the initialization
Pay attention to
- If all implementation ApplicationContextInitializer interface
- The smaller the Order value, the earlier the execution
- The one defined in application.properties takes precedence over the others
SpringFactoriesLoader introduction
The principle of
- A generic factory loading mechanism used within the framework
- Read files from specific locations of multiple JAR packages in the CLASspath and initialize classes
- The file content must be of KV type, i.e., properties type
- The key is the fully qualified name (abstract classes | interface), the value is achieved, multiple implementations use, segmentation
Framework initialization steps
SpringFactoriesLoader role
The SpringBoot framework reads specific files from the classpath JAR package to load the extended classes
LoadFactoies process
System initializer principle analysis
role
- Context refresh is called before the refresh method
- Used to encode and set some property variables commonly used in web environments
- You can sort through the Order interface
Call the point
Calling process
Realize the principle of
- The definition is found and registered by the SpringFactoriesLoader in the Spring. factories file
- Manually add SpringApplication after initialization
- Defined as environment variables was DelegatingApplicationContextInitializer found registration
The interview questions
What about the Spring Elements Loader?
The SpringBoot factory load class that SpringBoot uses to load extension points
How does the SpringFactoriesLoader load the factory class?
Read the specified files in the specified path, read them into property objects, iterate through the contents of the file in turn, assemble into class names and corresponding implementations, sort by order.
What does the system initializer do?
It’s actually a callback interface to the SpringBoot container to which we can define our properties.
When is the system initializer called?
Called in the prepareContext procedure during springBoot boot