In this part we'll discuss the most widely known annotations of Spring beans used to define different types of beans.
There are several ways beans can be configured in a Spring container
- Using XML configuration we can declare these.
- In a configuration class, we can declare beans using the @Bean annotation.
- We can mark the class with one of the annotations from the package org.springframework.stereotype, and leave the rest to scanning the components. It means that Spring framework will autodetect these classes for dependency injection when annotation-based configuration and classpath scanning is used
- @Component: to mark the beans as Spring’s managed components.
- @Controller: It indicates that the class is a web controller (spring MVC)
- @Service: to indicate that it’s holding the business logic
- @Repository: The class, whose behavior is to store, fetch or search data