Junior — Middle
At the application startup phase, what will be initialized first: XML configuration, Java code for configuration, or the @Configuration annotation?
sobes.tech AI
Answer from AI
The order of initialization of configurations when starting a Spring application is as follows:
- XML configurations: if used, they are loaded first, as this is the traditional way of configuring the Spring context.
@Configurationannotation: classes with this annotation are processed during context creation, and their beans are registered.- Java code for configuration: usually classes with
@Configurationand methods with@Bean, which are initialized during the context creation.
Thus, if there is XML configuration in the project, it is loaded first, then classes with @Configuration are processed, where Java configuration is initialized.
Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
If there is an XML file applicationContext.xml, it will be loaded before the beans from AppConfig are created.