Sobes.tech
Junior — Middle

What annotations are used to define and configure beans in the Spring Framework?

sobes.tech AI

Answer from AI

In the Spring Framework, the following annotations are most commonly used for defining and configuring beans:

  • @Component — a basic annotation for automatic detection and registration of a component as a bean.
  • @Service — a specialized annotation for the service layer, semantically indicating that the class contains business logic.
  • @Repository — used for the DAO layer, helps handle database-related exceptions.
  • @Controller — for components responsible for handling HTTP requests in MVC.
  • @Bean — used in methods of configuration classes (@Configuration) for explicit bean definition.
  • @Configuration — indicates that the class contains bean definitions via methods annotated with @Bean.

Example of using @Bean and @Configuration:

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

Thus, annotations help Spring to automatically or explicitly create and manage the lifecycle of beans.

What annotations are used to define and configure… - sobes.tech