Sobes.tech
Junior — Middle

Where and how are methods annotated with @Bean declared in a Spring application?

sobes.tech AI

Answer from AI

Methods annotated with @Bean are declared within classes marked with the @Configuration annotation in a Spring application. These methods define components (beans) that the Spring container will create and manage.

Example:

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

Here, the myService() method is annotated with @Bean and returns an object that will be registered in the Spring context as a bean named myService. This approach allows explicit configuration of dependencies and management of component lifecycle.

Where and how are methods annotated with @Bean… - sobes.tech