Junior — Middle
Can you list and explain the main annotations related to bean management in Spring?
sobes.tech AI
Answer from AI
In Spring, the main annotations used for managing beans are:
@Component— marks a class as a component to be automatically detected and registered in the Spring context.@Service— a specialized@Componentfor service layer, helps semantically distinguish roles of beans.@Repository— a specialized@Componentfor data access layer, supports database exception handling.@Controller— for marking controllers in MVC applications.@Autowired— used for automatic dependency injection into fields, constructors, or methods.@Scope— defines the scope of the bean (e.g., singleton, prototype).
Example:
@Component
public class MyComponent {
@Autowired
private MyService myService;
// ...
}
@Service
public class MyService {
// ...
}