This page provides a quick reference for the main Spring annotations and their usage, categorized by their purpose and role in a Spring application.
These annotations are used to mark classes as Spring-managed components.
@Component public class MyComponent { // General logic }
Annotations for classes that implement business logic.
@Service public class MyService { public void performLogic() { // Business logic } }
Annotations for classes that interact with the database.
@Repository public class MyRepository { public List<String> findAll() { // Database interaction return List.of("data1", "data2"); } }
Annotations for handling HTTP requests in web applications.
@Controller public class MyController { @GetMapping("/hello") public String sayHello() { return "hello"; } }
@RestController public class MyRestController { @GetMapping("/api/data") public List<String> getData() { return List.of("data1", "data2"); } }
Annotations for defining and managing Spring beans.
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } }
@Bean public MyComponent myComponent() { return new MyComponent(); }
Annotations for injecting dependencies into Spring components.
@Component public class MyComponent { private final MyService myService; @Autowired public MyComponent(MyService myService) { this.myService = myService; } }
@Component public class MyComponent { @Autowired @Qualifier("specificService") private MyService myService; }
@Component public class MyComponent { @Value("${app.name}") private String appName; }
Annotations for defining the scope of Spring beans.
@Component @Scope("prototype") public class MyPrototypeBean { // A new instance is created for each request }
Annotations for implementing cross-cutting concerns like logging or security.
@Aspect @Component public class LoggingAspect { @Before("execution(* com.example.*.*(..))") public void logBefore() { System.out.println("Logging before method execution"); } }
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.