User Tools

Site Tools


spring:spring-annotations

Spring Annotations Guide

This page provides a quick reference for the main Spring annotations and their usage, categorized by their purpose and role in a Spring application.

1. Core Component Annotations

These annotations are used to mark classes as Spring-managed components.

  • @Component
    1. General-purpose annotation for any Spring-managed component.
    2. Use this when the class does not fit into a specific role like `@Service` or `@Repository`.
    3. Example:
      @Component
      public class MyComponent {
          // General logic
      }
 

2. Service Layer Annotations

Annotations for classes that implement business logic.

  • @Service
    1. Specialized version of `@Component` for service classes.
    2. Indicates that the class contains business logic.
    3. Example:
      @Service
      public class MyService {
          public void performLogic() {
              // Business logic
          }
      }
 

3. Data Access Layer Annotations

Annotations for classes that interact with the database.

  • @Repository
    1. Specialized version of `@Component` for data access objects (DAO).
    2. Enables automatic exception translation for database-related exceptions.
    3. Example:
      @Repository
      public class MyRepository {
          public List<String> findAll() {
              // Database interaction
              return List.of("data1", "data2");
          }
      }
 

4. Web Layer Annotations

Annotations for handling HTTP requests in web applications.

  • @Controller
    1. Specialized version of `@Component` for web controllers.
    2. Handles HTTP requests and returns views.
    3. Example:
      @Controller
      public class MyController {
          @GetMapping("/hello")
          public String sayHello() {
              return "hello";
          }
      }
 
  • @RestController
    1. Combines `@Controller` and `@ResponseBody`.
    2. Used for REST APIs where methods return data (e.g., JSON).
    3. Example:
      @RestController
      public class MyRestController {
          @GetMapping("/api/data")
          public List<String> getData() {
              return List.of("data1", "data2");
          }
      }
 

5. Configuration Annotations

Annotations for defining and managing Spring beans.

  • @Configuration
    1. Marks a class as a source of bean definitions.
    2. Example:
      @Configuration
      public class AppConfig {
          @Bean
          public MyService myService() {
              return new MyService();
          }
      }
 
  • @Bean
    1. Used within `@Configuration` classes to define individual beans.
    2. Example:
      @Bean
      public MyComponent myComponent() {
          return new MyComponent();
      }
 

6. Dependency Injection Annotations

Annotations for injecting dependencies into Spring components.

  • @Autowired
    1. Automatically injects a dependency.
    2. Can be used on constructors, fields, or methods.
    3. Example:
      @Component
      public class MyComponent {
          private final MyService myService;
 
          @Autowired
          public MyComponent(MyService myService) {
              this.myService = myService;
          }
      }
 
  • @Qualifier
    1. Specifies which bean to inject when multiple candidates are available.
    2. Example:
      @Component
      public class MyComponent {
          @Autowired
          @Qualifier("specificService")
          private MyService myService;
      }
 
  • @Value
    1. Injects values from configuration files (e.g., `application.properties`).
    2. Example:
      @Component
      public class MyComponent {
          @Value("${app.name}")
          private String appName;
      }
 

7. Scope Annotations

Annotations for defining the scope of Spring beans.

  • @Scope
    1. Specifies the scope of a bean (`singleton`, `prototype`, etc.).
    2. Example:
      @Component
      @Scope("prototype")
      public class MyPrototypeBean {
          // A new instance is created for each request
      }
 

8. Aspect-Oriented Programming (AOP) Annotations

Annotations for implementing cross-cutting concerns like logging or security.

  • @Aspect
    1. Marks a class as an aspect for AOP.
    2. Example:
      @Aspect
      @Component
      public class LoggingAspect {
          @Before("execution(* com.example.*.*(..))")
          public void logBefore() {
              System.out.println("Logging before method execution");
          }
      }
 

Summary Table

Role Annotation Description
————————-————————————————————————-
General Component `@Component` General-purpose Spring-managed component.
Business Logic `@Service` Marks a service class for business logic.
Data Access `@Repository` Marks a DAO or repository class.
Web Controller `@Controller` Handles HTTP requests and returns views.
REST API Controller `@RestController` Handles REST API requests and returns data.
Configuration `@Configuration` Defines Spring beans and configuration.
Bean Definition `@Bean` Defines a bean explicitly in a configuration class.
Dependency Injection `@Autowired` Injects dependencies automatically.
Dependency Selection `@Qualifier` Specifies which bean to inject.
Configuration Value `@Value` Injects values from configuration files.
Bean Scope `@Scope` Defines the scope of a bean.
Aspect `@Aspect` Marks a class as an AOP aspect.

This guide provides a structured overview of the most commonly used Spring annotations to help you quickly identify and apply them in your projects.

spring/spring-annotations.txt · Last modified: 2025/02/07 12:09 by odefta