Junior — Middle
What annotations in Spring are used for configuring and setting up application components?
sobes.tech AI
Answer from AI
In Spring, the following main annotations are used for configuring and setting up application components:
@Component— marks a class as a Spring component to be managed by the container.@Service— a specialized component for business logic.@Repository— for data access components.@Controller— for controllers in MVC.@Configuration— a class containing bean definitions.@Bean— a method within@Configurationthat creates a bean.@Autowired— for automatic dependency injection.
Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
@Component
public class MyComponent {
@Autowired
private MyService myService;
}