Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
In Spring, stereotype annotations are used to mark classes so that they can be recognized by Spring and automatically registered as beans in the Spring application context. These annotations also indicate the layer or the "role" the annotated component plays in the larger application context. Here are the main stereotype annotations:
@Component:
@Component
, Spring will auto-detect and auto-register it as a bean.@Component public class GenericService { // ... }
@Repository:
@Repository public class UserDao { // Database related operations }
@Service:
@Component
, but we use it to indicate the specific purpose of the class.@Service public class UserService { // Business service operations }
@Controller:
@Controller public class UserController { // Handle web requests }
@RestController:
@Controller
annotation. It's used for creating RESTful web services in Spring. Classes annotated with @RestController
have their returned data automatically serialized as JSON or XML.@RestController public class UserRestController { // Handle RESTful web requests }
For Spring to auto-detect these beans, you need to enable component scanning in your configuration. You can do this via XML with:
<context:component-scan base-package="com.example.package"/>
Or, if you're using Java configuration:
@Configuration @ComponentScan(basePackages = "com.example.package") public class AppConfig { // ... }
Using stereotype annotations helps in providing a clear role-based distinction of components in your application, making the codebase more organized and maintainable.
@Component, @Service, @Repository in Spring:
@Component
, @Service
, @Repository
) in Spring for bean definition.@Component public class MyComponent { // Class implementation } @Service public class MyService { // Class implementation } @Repository public class MyRepository { // Class implementation }
Custom Stereotype Annotations in Spring:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface MyCustomComponent { // Additional attributes or methods if needed } @MyCustomComponent public class MyCustomBean { // Class implementation }
Injection of Stereotype-annotated beans in Spring:
@Service public class MyService { @Autowired private MyRepository myRepository; // Class implementation }
Conditional bean creation with Stereotype Annotations in Spring:
@ConditionalOnProperty(name = "my.feature.enabled", havingValue = "true") @Service public class ConditionalService { // Class implementation }
Annotation-based configuration with Stereotype Annotations in Spring:
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } }
Combining Stereotype Annotations in Spring components:
@Service @Transactional public class TransactionalService { // Class implementation }
Customizing bean names with Stereotype Annotations in Spring:
@Component("customBeanName") public class MyCustomBean { // Class implementation }