Sobes.tech
Junior — Middle

Can you explain the purpose of the @Configuration annotation and how it is used in the context of Spring?

sobes.tech AI

Answer from AI

The @Configuration annotation in Spring is used to denote a class that contains bean definitions and application configuration. Such a class replaces traditional XML configurations and allows for programmatic creation and configuration of application components.

When Spring sees a class with @Configuration, it processes methods annotated with @Bean and creates the corresponding objects in the application context. This simplifies dependency management and enables the use of Java code for configuration.

Example:

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

Here, AppConfig is a configuration class, and the myService() method creates a MyService bean, which can be injected into other components via @Autowired.

Can you explain the purpose of the @Configuration… - sobes.tech