What are some of the important annotations in Spring Framework?

Here are some of the important annotations in the Spring Framework:

  1. @Component:
  2. Purpose: Marks a class as a Spring component.
  3. Usage: Indicates that the class is a Spring-managed bean.
  4. Example:

java @Component public class MyComponent { // class body }

  1. @Service:
  2. Purpose: Specializes the @Component annotation to indicate a service component.
  3. Usage: Used for service layer components.
  4. Example:

java @Service public class MyService { // service methods }
5. @Repository:
6. Purpose: Specializes the @Component annotation to indicate a data repository.
7. Usage: Used for DAO (Data Access Object) components.
8. Example:

java @Repository public class MyRepository { // database interaction methods }
9. @Controller:
10. Purpose: Specializes the @Component annotation to indicate a Spring MVC controller.
11. Usage: Used for controller components in the web layer.
12. Example:

java @Controller public class MyController { // handler methods }
13. @RestController:
14. Purpose: Combines @Controller and @ResponseBody annotations.
15. Usage: Used for RESTful web services.
16. Example:

java @RestController public class MyRestController { @GetMapping("/greeting") public String greet() { return "Hello, World!"; } }
17. @Autowired:
18. Purpose: Enables automatic dependency injection.
19. Usage: Used on fields, constructors, or methods.
20. Example:

java @Service public class MyService { @Autowired private MyRepository myRepository; // service methods }
21. @Value:
22. Purpose: Injects values from property files.
23. Usage: Used on fields or method parameters.
24. Example:

java @Component public class MyComponent { @Value("${my.property}") private String myProperty; // class body }
25. @Configuration:
26. Purpose: Indicates that a class declares one or more @Bean methods.
27. Usage: Used to define configuration classes.
28. Example:

java @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } }
29. @Bean:
30. Purpose: Indicates that a method produces a bean to be managed by the Spring container.
31. Usage: Used within @Configuration classes.
32. Example:

java @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } }
33. @Scope:

* **Purpose**: Specifies the scope of a bean.
* **Usage**: Used in conjunction with `@Component` or `@Bean`.
* **Example**:

java @Component @Scope("prototype") public class MyPrototypeBean { // class body }

  1. @Qualifier:

    • Purpose: Resolves ambiguity when multiple beans of the same type exist.
    • Usage: Used alongside @Autowired.
    • Example:

    java @Service public class MyService { @Autowired @Qualifier("specificBean") private MyBean myBean; // service methods }
    35. @Transactional:

    • Purpose: Manages transactions for a method or class.
    • Usage: Used on service layer methods or classes.
    • Example:

    java @Service public class MyService { @Transactional public void performTransaction() { // transactional code } }
    36. @EnableAutoConfiguration:

    • Purpose: Enables Spring Boot’s auto-configuration feature.
    • Usage: Typically used in the main application class.
    • Example:

    java @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }