转自:https://www.baeldung.com/running-setup-logic-on-startup-in-spring
1. Introduction
In this article we’ll focus on how to run logic at the startup of a Spring application.
2. Running Logic On Startup
Running logic during/after Spring application’s startup is a common scenario, but one that causes multiple problems.
In order to benefit from Inverse of Control, we naturally need to renounce partial control over the application’s flow to the container – which is why instantiation, setup logic on startup, etc needs special attention.
We can’t simply include our logic in the beans’ constructors or call methods after instantiation of any object; we are simply not in control during those processes.
Let’s look at the real-life example:
1 2 3 4 5 6 7 8 9 10 |
|
Here, we’re trying to access an autowired field in the constructor. When the constructor is called, the Spring bean is not yet fully initialized. This is problematic because calling not yet initialized fields will of course result inNullPointerExceptions.
Spring gives us a few ways of managing this situation.
2.1. The @PostConstruct Annotation
Javax’s @PostConstruct annotation can be used for annotating a method that should be run once immediately after the bean’s initialization. Keep in mind that the annotated method will be executed by Spring even if there is nothing to inject.
Here’s @PostConstruct in action:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
In the example above you can see that the Environment instance was safely injected and then called in the @PostConstruct annotated method without throwing a NullPointerException.
2.2. The InitializingBean Interface
The InitializingBean approach works pretty similarly to the previous one. Instead of annotating a method, you need to implement the InitializingBean interface and the afterPropertiesSet() method.
Here you can see the previous example implemented using the InitializingBean interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
2.3. An ApplicationListener
This approach can be used for running logic after the Spring context has been initialized, so we are not focusing on any particular bean, but waiting for all of them to initialize.
In order to achieve this you need to create a bean that implements the ApplicationListener<ContextRefreshedEvent>interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The same results can be achieved by using the newly-introduced @EventListener annotation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
In this example we chose the ContextRefreshedEvent. Make sure to pick an appropriate event that suits your needs.
2.4. The @Bean initMethod attribute
The initMethod property can be used to execute a method after a bean’s initialization.
Here’s what a bean looks like:
1 2 3 4 5 6 7 8 9 10 11 |
|
You can notice that there are no special interfaces implemented nor any special annotations used.
Then, we can define the bean using the @Bean annotation:
1 2 3 4 |
|
And this is how a bean definition looks in an XML config:
1 2 3 4 |
|
2.5. Constructor Injection
If you are injecting fields using Constructor Injection, you can simply include your logic in a constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
2.6. Spring Boot CommandLineRunner
Spring boot provides a CommanLineRunner interface with a callback run() method which can be invoked at application startup after the Spring application context is instantiated.
Let’s look at an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Full source code can be found on Github.
Note: As mentioned in the documentation, multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the @Ordered interface or @Order annotation.
2.7. Spring Boot ApplicationRunner
Similar to CommandLineRunner, Spring boot also provides an ApplicationRunner interface with a run() method to be invoked at application startup. However, instead of raw String arguments passed to the callback method, we have an instance of the ApplicationArguments class.
The ApplicationArguments interface has methods to get argument values that are options and plain argument values. An argument that is prefixed with – – is an option argument.
Let’s look at an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Full source code can be found on Github.
3. Combining Mechanisms
In order to achieve full control over your beans, you might want to combine the above mechanisms together.
The order of execution is as follows:
- The constructor
- the @PostConstruct annotated methods
- the InitializingBean’s afterPropertiesSet() method
- the initialization method specified as init-method in XML
Let’s create a Spring bean that combines all mechanisms:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
If you try to instantiate this bean, you will be able to see logs that match the order specified above:
1 2 3 4 |
|