1. Various ways to automatically execute methods after SpringBoot is started
1.1 @PostConstruct Annotation
Function: Execute the initialization method after the dependency injection is completed.
Applicable scenarios: Some operations (such as configuration, preload data) are required when bean is initialized.
Note: This method is executed during the bean initialization phase, and the application may not be fully started (such as the database connection is not ready).
Sample code:
import ; import ; @Component public class MyPostConstructBean { @PostConstruct public void init() { ("PostConstruct method execution"); // Execute initialization logic } }
1.2. Implement the InitializingBean interface
Function: Initialization is achieved by rewriting the afterPropertiesSet method.
Applicable scenarios: Operations need to be performed after attribute injection, similar to @PostConstruct but requires an interface to be implemented.
Sample code:
import ; import ; @Component public class MyInitializingBean implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { ("InitializingBean's afterPropertiesSet method execution"); // Execute initialization logic } }
1.3. Implement the ApplicationRunner interface
Function: Execute after the Spring application context is refreshed, and supports receiving command line parameters.
Applicable scenario: The operation needs to be performed after the application is started, and the bean has been initialized at this time.
Sample code:
import ; import ; import ; @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { ("ApplicationRunner Execution"); // Execution logic, access command line parameters args } }
1.4. Implement the CommandLineRunner interface
Function: Similar to ApplicationRunner, but with the parameter String[].
Applicable scenario: Used when you need to receive simple command line parameters.
Sample code:
import ; import ; @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { ("CommandLineRunner Execution"); // Execution logic, accessing args parameters } }
1.5. Implement the ApplicationListener interface
Function: Listen to the application full startup event through ApplicationListener.
Applicable scenarios: Actions need to be performed after the application is fully ready (such as sending notifications after startup).
Sample code:
import ; import ; import ; @Component public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(ApplicationReadyEvent event) { ("ApplicationReadyEvent triggered, application started"); // Execution logic, at this time, all beans can be accessed safely } }
1.6. Use @Bean's initMethod property
Function: When defining a bean in the Spring configuration class, specify the initialization method through initMethod.
Sample code:
import ; import ; @Configuration public class AppConfig { @Bean(initMethod = "init") public MyBean myBean() { return new MyBean(); } } public class MyBean { public void init() { ("Execute after application starts: initMethod"); // Add initialization logic here } }
Or use the init-method property (XML configuration):
<bean class="" init-method="init"/>
2. Comparison of execution timings of various methods
method | Execution timing |
---|---|
@PostConstruct | After the bean initialization is completed (after attribute injection) |
InitializingBean | Similar to @PostConstruct, after property injection |
ApplicationRunner | After the application context is refreshed, before ApplicationReadyEvent is triggered |
CommandLineRunner | Same as above |
ApplicationListener | The application is fully ready (all beans are initialized and all startup tasks are executed) |
Choose the right method according to your needs:
- When you need to access a database or resource, it is recommended to use ApplicationListener or CommandLineRunner.
- Simple bean initialization logic is available with @PostConstruct or InitializingBean.
Things to note
- Threading problem: By default, it is executed on the main thread to avoid long-term blocking operations. Asynchronous execution can be considered (such as combined with @Async).
- Dependency injection: No other beans can be accessed directly in @PostConstruct and InitializingBeans (need to wait for initialization to complete).
- Sequence control: Control the order of multiple startup tasks through @Order or implementing the Ordered interface.
Related Documents:Detailed explanation of the use of InitializingBean interface and @PostConstruct annotation in Spring
3. Use @Order to control the execution order
Function: Control the execution order of multiple startup tasks through @Order or implementing the Ordered interface.
Example: Using @Order in ApplicationRunner
import ; import ; import ; @Component @Order(1) // The smaller the value, the higher the prioritypublic class FirstRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) { ("The first runner to execute"); } } @Component @Order(2) public class SecondRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) { ("The second execution runner"); } }
This is the article about the comparison of various methods of automatic execution after SpringBoot starts. For more information about the automatic execution method of SpringBoot starts, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!