====== 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**
- General-purpose annotation for any Spring-managed component.
- Use this when the class does not fit into a specific role like `@Service` or `@Repository`.
- Example:
@Component
public class MyComponent {
// General logic
}
===== 2. Service Layer Annotations =====
Annotations for classes that implement business logic.
* **@Service**
- Specialized version of `@Component` for service classes.
- Indicates that the class contains business logic.
- Example:
@Service
public class MyService {
public void performLogic() {
// Business logic
}
}
===== 3. Data Access Layer Annotations =====
Annotations for classes that interact with the database.
* **@Repository**
- Specialized version of `@Component` for data access objects (DAO).
- Enables automatic exception translation for database-related exceptions.
- Example:
@Repository
public class MyRepository {
public List findAll() {
// Database interaction
return List.of("data1", "data2");
}
}
===== 4. Web Layer Annotations =====
Annotations for handling HTTP requests in web applications.
* **@Controller**
- Specialized version of `@Component` for web controllers.
- Handles HTTP requests and returns views.
- Example:
@Controller
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "hello";
}
}
* **@RestController**
- Combines `@Controller` and `@ResponseBody`.
- Used for REST APIs where methods return data (e.g., JSON).
- Example:
@RestController
public class MyRestController {
@GetMapping("/api/data")
public List getData() {
return List.of("data1", "data2");
}
}
===== 5. Configuration Annotations =====
Annotations for defining and managing Spring beans.
* **@Configuration**
- Marks a class as a source of bean definitions.
- Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
* **@Bean**
- Used within `@Configuration` classes to define individual beans.
- Example:
@Bean
public MyComponent myComponent() {
return new MyComponent();
}
===== 6. Dependency Injection Annotations =====
Annotations for injecting dependencies into Spring components.
* **@Autowired**
- Automatically injects a dependency.
- Can be used on constructors, fields, or methods.
- Example:
@Component
public class MyComponent {
private final MyService myService;
@Autowired
public MyComponent(MyService myService) {
this.myService = myService;
}
}
* **@Qualifier**
- Specifies which bean to inject when multiple candidates are available.
- Example:
@Component
public class MyComponent {
@Autowired
@Qualifier("specificService")
private MyService myService;
}
* **@Value**
- Injects values from configuration files (e.g., `application.properties`).
- Example:
@Component
public class MyComponent {
@Value("${app.name}")
private String appName;
}
===== 7. Scope Annotations =====
Annotations for defining the scope of Spring beans.
* **@Scope**
- Specifies the scope of a bean (`singleton`, `prototype`, etc.).
- 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**
- Marks a class as an aspect for AOP.
- 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.