In the Spring framework, various annotations are used to simplify development by providing different configurations, components, and functionalities. Below is a comprehensive list of the key Spring annotations across Spring Core, Spring Boot, and Spring MVC with their descriptions:
1. Spring Core Annotations:
These are the basic annotations used in Spring for dependency injection and configuration.
a) @Component
- Marks a class as a Spring bean (i.e., it will be automatically registered in the application context).
b) @Service
- A specialization of
@Componentto mark a service class, typically used in the service layer.
c) @Repository
- A specialization of
@Componentused to define a DAO (Data Access Object) bean. It provides additional persistence-related functionality like exception translation.
d) @Controller
- Marks a class as a Spring MVC controller, typically used in web applications to handle HTTP requests.
e) @Autowired
- Used for dependency injection. Spring automatically wires the bean by searching the context for the matching type.
f) @Value
- Injects values into fields from properties files or environment variables.
g) @Qualifier
- Used in conjunction with
@Autowiredto specify which bean to inject when multiple beans of the same type exist.
h) @Bean
- Defines a bean explicitly in a configuration class (usually used in
@Configurationclasses).
i) @Configuration
- Indicates that a class contains Spring configuration and bean definitions.
j) @Scope
- Specifies the scope of a Spring bean (e.g., singleton, prototype, etc.).
k) @PostConstruct
- Marks a method to be called after the bean's properties are set, commonly used for initialization.
l) @PreDestroy
- Marks a method to be called before the bean is destroyed (clean-up logic).
2. Spring Boot Annotations:
Spring Boot annotations simplify Spring application configuration and management.
a) @SpringBootApplication
- A convenience annotation that combines
@Configuration,@EnableAutoConfiguration, and@ComponentScan. It is used to mark the main class of a Spring Boot application.
b) @EnableAutoConfiguration
- Tells Spring Boot to automatically configure the application based on the dependencies available in the classpath.
c) @ComponentScan
- Tells Spring to scan for components in the specified package. This is often used in conjunction with
@SpringBootApplication.
d) @ConfigurationProperties
- Binds properties defined in
application.propertiesorapplication.ymlto a Java bean.
e) @SpringBootTest
- Used to write integration tests with Spring Boot, loading the full application context.
f) @RestController
- A convenience annotation that combines
@Controllerand@ResponseBodyfor creating REST APIs.
g) @Value
- Injects a value from
application.propertiesor environment variables.
3. Spring MVC Annotations:
Spring MVC provides several annotations to handle web requests and responses.
a) @RequestMapping
- Maps HTTP requests to handler methods of MVC controllers.
b) @GetMapping
- A shortcut for
@RequestMapping(method = RequestMethod.GET)for GET requests.
c) @PostMapping
- A shortcut for
@RequestMapping(method = RequestMethod.POST)for POST requests.
d) @PutMapping
- A shortcut for
@RequestMapping(method = RequestMethod.PUT)for PUT requests.
e) @DeleteMapping
- A shortcut for
@RequestMapping(method = RequestMethod.DELETE)for DELETE requests.
f) @RequestParam
- Used to extract query parameters from the request URL.
g) @PathVariable
- Extracts values from the URI path.
h) @RequestBody
- Binds the incoming HTTP request body to a method parameter.
i) @ResponseBody
- Used to indicate that the return value of the method should be written directly to the HTTP response body (often used in REST controllers).
j) @ModelAttribute
- Binds request parameters to a model object, typically used in form handling
k) @ExceptionHandler
- Handles exceptions thrown by request-handling methods.
Comments
Post a Comment