The aim of this PoC is to better understand the order in which things gets initialized in a spring boot application. The application has
- a InitializationApplication SpringBootApplication
- a RuntimeBean Component
- a InitializationApplicationTests SpringBootTest
- a TestBean Bean declared in the TestConfiguration
mvn test
By checking the logs we can see that the initialization order is as follows:
Test class
- initialization methods
- @BeforeAll method
- constructor
Runtime component
- static initialization methods
- constructor
- @PostConstruct method
Test component
- static initialization methods
- constructor
- @PostConstruct method
Test class
- @PostConstruct method
- @BeforeEach method
- @AfterEach method
(Note that no static methods are called during context reloading)
Runtime component
- constructor
- @PostConstruct method
Test component
- constructor
- @PostConstruct method
Test class
- @PostConstruct method
- @BeforeEach method
- @AfterEach method