To be precise, several initialization methods are completed after the instantiation of the spring container. Why do you say so?
Here is an example:
@Slf4j @Component public class InitBeanDemo { @Autowired private Environment env; public InitBeanDemo() { ("DefaultProfiles: {}", (())); ("ActiveProfiles: {}", (())); }
The example is to do some initialization work in the bean construction method. The example is relatively simple and only does log printing. It’s the ideal is full, the reality is skinny, and the error is reported:
Constructor threw exception; nested exception is
The reason is that Environment has not been initialized yet.
Next, let’s explore what initialization methods can meet the needs of the above example.
Initialization in the construction method
It can run normally and has the earliest execution time among all initialization methods.
The principle is to instantiate the Environment before InitBeanDemo is instantiated.
@Component public class InitBeanDemo { private final Environment env; @Autowired public InitBeanDemo (Environment environment) { = environment; ("Constructor DefaultProfiles: {}", (())); ("Constructor ActiveProfiles: {}", (())); } }
Regular three-piece set
Regular three-piece set: @PostConstruct, InitializingBean, initMethod.
If you prefer, the three ways can be used simultaneously under the same bean, priority of execution @PostConstruct > InitializingBean > initMethod.
@PostConstruct annotation
In a bean that can be scanned into, add a public void xxx() method and add @PostConstruct annotation to write the logic that needs to be initialized.
There can be multiple @PostConstruct annotations in the same application, and there can also be multiple @PostConstruct annotations in the same bean.
@Slf4j @Component public class InitBeanDemo { @Autowired private Environment env; @PostConstruct public void init() { ("@PostConstruct DefaultProfiles: {}", (())); ("@PostConstruct ActiveProfiles: {}", (())); }
Implement the InitializingBean interface
Implement the InitializingBean interface and write the logic that needs to be initialized in the afterPropertiesSet() method.
There can be multiple classes that implement the InitializingBean interface in the same application, and the execution opportunities are sorted in the natural order of the class names.
@Slf4j @Component public class InitBeanDemo implements InitializingBean { @Autowired private Environment env; @Override public void afterPropertiesSet() throws Exception { ("InitializingBean DefaultProfiles: {}", (())); ("InitializingBean ActiveProfiles: {}", (())); } }
Specify the initMethod method of the bean
Use @Bean annotationinitMethodAttributes can be used for methods executed after initialization of a bean.
initMethod must be a parameterless constructor of public void.
@Slf4j public class InitBeanDemo implements InitializingBean { @Autowired private Environment env; public void initMethod() { ("initMethod DefaultProfiles: {}", (())); ("initMethod ActiveProfiles: {}", (())); }
@Configuration public class InitBeanConfig { @Bean(initMethod="initMethod") public InitBeanDemo initBeanDemo () { return new InitBeanDemo(); } }
Equivalent to the XML configurationinit-methodproperty:
<bean class="" init-method="initMethod"></bean>
Custom ApplicationListener Listening
There are two ways, one implements the interface and the other uses annotations.
Implement the ApplicationListener interface
Listen to the ContextRefreshedEvent event.
@Slf4j @Component public class InitBeanDemo implements ApplicationListener<ContextRefreshedEvent>{ @Autowired private Environment env; @Override public void onApplicationEvent(ContextRefreshedEvent event) { ("ApplicationListener DefaultProfiles: {}", (())); ("ApplicationListener ActiveProfiles: {}", (())); } }
@EventListener annotation
Specify the ContextRefreshedEvent event in the method parameter.
@Slf4j @Component public class InitBeanDemo { @Autowired private Environment env; @EventListener public void onApplicationEvent2(ContextRefreshedEvent event) { ("@EventListener DefaultProfiles: {}", (())); ("@EventListener ActiveProfiles: {}", (())); } }
Initialization interface provided by Spring Boot
ApplicationRunner interface
@Slf4j @Component public class InitBeanDemo implements ApplicationRunner { @Autowired private Environment env; @Override public void run(ApplicationArguments args) throws Exception { ("ApplicationRunner: {}", args); ("ApplicationRunner: {}", ()); ("ApplicationRunner DefaultProfiles: {}", (())); ("ApplicationRunner ActiveProfiles: {}", (())); } }
CommandLineRunner interface
Multiple CommandLineRunner beans can be defined in the same application context and can be sorted using the @Ordered interface or the @Order annotation.
@Slf4j @Component public class InitBeanDemo implements CommandLineRunner { @Autowired private Environment env; @Override public void run(String... args) throws Exception { ("CommandLineRunner: {}", args); ("CommandLineRunner DefaultProfiles: {}", (())); ("CommandLineRunner ActiveProfiles: {}", (())); } }
Use the above initialization method to execute order in the same bean
Use the above initialization method in the same bean to run the log fragment:
2021-06-07 11:24:41|INFO |main||Constructor DefaultProfiles: [default]
2021-06-07 11:24:41|INFO |main||Constructor ActiveProfiles: [sit]2021-06-07 11:24:42|INFO |main||@PostConstruct DefaultProfiles: [default]
2021-06-07 11:24:42|INFO |main||@PostConstruct ActiveProfiles: [sit]
2021-06-07 11:24:42|INFO |main||InitializingBean DefaultProfiles: [default]
2021-06-07 11:24:42|INFO |main||InitializingBean ActiveProfiles: [sit]
2021-06-07 11:24:42|INFO |main||initMethod DefaultProfiles: [default]
2021-06-07 11:24:42|INFO |main||initMethod ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main||@EventListener DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main||@EventListener ActiveProfiles: [sit]
2021-06-07 11:24:44|INFO |main||ApplicationListener DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main||ApplicationListener ActiveProfiles: [sit]
2021-06-07 11:24:44|INFO |main||ApplicationRunner: @68bef3df
2021-06-07 11:24:44|INFO |main||ApplicationRunner: []
2021-06-07 11:24:44|INFO |main||ApplicationRunner DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main||ApplicationRunner ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main||CommandLineRunner: {}
2021-06-07 11:24:44|INFO |main||CommandLineRunner DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main||CommandLineRunner ActiveProfiles: [sit]
That is, the order of sorting out the entire article.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.