Sobes.tech
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:

  1. XML configurations: if used, they are loaded first, as this is the traditional way of configuring the Spring context.
  2. @Configuration annotation: classes with this annotation are processed during context creation, and their beans are registered.
  3. Java code for configuration: usually classes with @Configuration and 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.

At the application startup phase, what will be… - sobes.tech