Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Stereotype Annotations

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:

  1. @Component:

    • This is a generic stereotype annotation indicating that a class is a Spring component. When you annotate a class with @Component, Spring will auto-detect and auto-register it as a bean.
    • Example:
    @Component
    public class GenericService {
        // ...
    }
    
  2. @Repository:

    • This is used on classes that interact with the database. It's a marker for any class that fulfills the role of a repository (or Data Access Object).
    • It can also translate database-related exceptions into Spring's DataAccessException.
    • Example:
    @Repository
    public class UserDao {
        // Database related operations
    }
    
  3. @Service:

    • This indicates that a class provides business logic in the service layer.
    • It's technically identical to @Component, but we use it to indicate the specific purpose of the class.
    • Example:
    @Service
    public class UserService {
        // Business service operations
    }
    
  4. @Controller:

    • This is typically used with classes that act as MVC controllers in web applications.
    • Example:
    @Controller
    public class UserController {
        // Handle web requests
    }
    
  5. @RestController:

    • This is a specialized version of the @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.
    • Example:
    @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.

  1. @Component, @Service, @Repository in Spring:

    • Description: Introduces the commonly used Stereotype Annotations (@Component, @Service, @Repository) in Spring for bean definition.
    • Code:
      @Component
      public class MyComponent {
          // Class implementation
      }
      
      @Service
      public class MyService {
          // Class implementation
      }
      
      @Repository
      public class MyRepository {
          // Class implementation
      }
      
  2. Custom Stereotype Annotations in Spring:

    • Description: Illustrates the creation and usage of custom Stereotype Annotations in Spring.
    • Code:
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.TYPE)
      @Component
      public @interface MyCustomComponent {
          // Additional attributes or methods if needed
      }
      
      @MyCustomComponent
      public class MyCustomBean {
          // Class implementation
      }
      
  3. Injection of Stereotype-annotated beans in Spring:

    • Description: Demonstrates how to inject beans annotated with Stereotype Annotations into other Spring components.
    • Code:
      @Service
      public class MyService {
          @Autowired
          private MyRepository myRepository;
      
          // Class implementation
      }
      
  4. Conditional bean creation with Stereotype Annotations in Spring:

    • Description: Shows how to conditionally create beans using Stereotype Annotations in Spring.
    • Code:
      @ConditionalOnProperty(name = "my.feature.enabled", havingValue = "true")
      @Service
      public class ConditionalService {
          // Class implementation
      }
      
  5. Annotation-based configuration with Stereotype Annotations in Spring:

    • Description: Shows how to use Stereotype Annotations for annotation-based configuration in Spring.
    • Code:
      @Configuration
      public class AppConfig {
          @Bean
          public MyService myService() {
              return new MyService();
          }
      }
      
  6. Combining Stereotype Annotations in Spring components:

    • Description: Demonstrates how to combine multiple Stereotype Annotations on a single Spring component.
    • Code:
      @Service
      @Transactional
      public class TransactionalService {
          // Class implementation
      }
      
  7. Customizing bean names with Stereotype Annotations in Spring:

    • Description: Shows how to customize bean names when using Stereotype Annotations in Spring.
    • Code:
      @Component("customBeanName")
      public class MyCustomBean {
          // Class implementation
      }