Introduction
Spring Framework is a powerful and widely-used framework for building Java-based enterprise applications. It simplifies the development process by providing a range of features such as dependency injection, aspect-oriented programming, and transaction management. One of the key elements that make Spring so flexible and developer-friendly is its extensive use of annotations. In this blog post, we will explore some of the most commonly used Spring annotations and how they can be leveraged to enhance your Spring-based applications.
What are spring annotations?
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.
Types of spring annotations
- Spring Core Annotations
- Spring Stereotype Annotations
- Spring Data Annotations
- Spring MVC and REST Annotations
- Composed Annotations (@ RequestMapping Variants)
- Spring Boot Annotations
- Spring Testing Annotations
Spring Core Annotations
1. @ Autowired
This annotation is applied on fields, setter methods, and constructors. This annotation injects object dependency implicitly.
2. @ Configuration
This annotation is used on classes which define beans. @ Configuration is an analog for XML configuration file – it is configuration using Java class. Java class annotated with @ Configuration is a configuration by itself and will have methods to instantiate and configure the dependencies.
3. @ ComponentScan
This annotation is used with @ Configuration annotation to allow Spring to know the packages to scan for annotated components. @ ComponentScan is also used to specify base packages using basePackageClasses or basePackage attributes to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.
4. @ Bean
This annotation is used at the method level. @ Bean annotation works with @ Configuration to create Spring beans. As mentioned earlier, @ Configuration will have methods to instantiate and configure dependencies. Such methods will be annotated with @ Bean. The method annotated with this annotation works as bean ID and it creates and returns the actual bean.
5. @ Lazy
This annotation is used on component classes. By default all autowired dependencies are created and configured at startup. But if you want to initialize a bean lazily, you can use @ Lazy annotation over the class. This means that the bean will be created and initialized only when it is first requested for. You can also use this annotation on @ Configuration classes. This indicates that all @ Bean methods within that @ Configuration should be lazily initialized.
6. @ Value
This annotation is used at the field, constructor parameter, and method parameter level. The @ Value annotation indicates a default value expression for the field or parameter to initialize the property with. As the @ Autowired annotation tells Spring to inject object into another when it loads your application context, you can also use @ Value annotation to inject values from a property file into a bean’s attribute. It supports both #{...} and ${...} placeholders.
7. @ Qualifier
This annotation is used along with @ Autowired annotation. When you need more control of the dependency injection process, @ Qualifier can be used. @ Qualifier can be specified on individual constructor arguments or method parameters. This annotation is used to avoid confusion which occurs when you create more than one bean of the same type and want to wire only one of them with a property.
8. @ Primary
The @ Primary annotation is a powerful and useful annotation provided by the Spring Framework. It is used to indicate a primary bean when there are multiple beans of the same type defined in the Spring application context. The primary bean is the one that should be used by default when the type is autowired or injected into other components.
Spring Stereotype Annotations
1. @ Component
This annotation is used on classes to indicate a Spring component. The @ Component annotation marks the Java class as a bean or say component so that the component-scanning mechanism of Spring can add into the application context.
2. @ Controller
The @ Controller annotation is used to indicate the class is a Spring controller. This annotation can be used to identify controllers for Spring MVC or Spring WebFlux.
3. @ Service
This annotation is used on a class. The @ Service marks a Java class that performs some service, such as execute business logic, perform calculations and call external APIs. This annotation is a specialized form of the @ Component annotation intended to be used in the service layer.
4. @ Repository
This annotation is used on Java classes which directly access the database. The @ Repository annotation works as marker for any class that fulfills the role of repository or Data Access Object. This annotation has a automatic translation feature. For example, when an exception occurs in the @ Repository there is a handler for that exception and there is no need to add a try catch block.
Spring Data Annotations
1. @ Entity
The @ Entity annotation is fundamental in Spring Data's JPA (Java Persistence API) support. It designates a Java class as an entity, indicating that instances of this class can be mapped to rows in a relational database table.
2. @ Query
While standard CRUD methods are powerful, custom queries are often necessary for complex data retrieval. The @ Query annotation allows you to define custom queries using JPQL (Java Persistence Query Language) or native SQL.
3. @ Transactional
The @ Transactional annotation is pivotal for managing transactions. You can apply it at the class or method level to specify the scope of a single database transaction. When used on a method, it ensures that the method executes within a transactional context.
4. @ Column
The @ Column annotation allows you to customize the mapping of entity fields to database columns. This annotation is particularly useful when you need to specify column names, types, or constraints
5. @ Table
The @ Table annotation is used to customize the mapping of an entity to a specific database table. You can specify the table name and other attributes using this annotation.
Spring MVC and REST Annotations
1. @ Controller
This annotation is used on Java classes that play the role of controller in your application. The @ Controller annotation allows autodetection of component classes in the classpath and auto-registering bean definitions for them. To enable autodetection of such annotated controllers, you can add component scanning to your configuration. The Java class annotated with @ Controller is capable of handling multiple request mappings. This annotation can be used with Spring MVC and Spring WebFlux.
2. @ RequestMapping
This annotation is used both at class and method level. The @ RequestMapping annotation is used to map web requests onto specific handler classes and handler methods. When @ RequestMapping is used on class level it creates a base URI for which the controller will be used. When this annotation is used on methods it will give you the URI on which the handler methods will be executed. From this you can infer that the class level request mapping will remain the same whereas each handler method will have their own request mapping. Sometimes you may want to perform different operations based on the HTTP method used, even though the request URI may remain the same. In such situations, you can use the method attribute of @ RequestMapping with an HTTP method value to narrow down the HTTP methods in order to invoke the methods of your class.
3. @ CookieValue
This annotation is used at method parameter level. @ CookieValue is used as argument of request mapping method. The HTTP cookie is bound to the @ CookieValue parameter for a given cookie name. This annotation is used in the method annotated with @ RequestMapping.
4. @ CrossOrigin
This annotation is used both at class and method level to enable cross origin requests. In many cases the host that serves JavaScript will be different from the host that serves the data. In such a case Cross Origin Resource Sharing (CORS) enables cross-domain communication. To enable this communication you just need to add the @ CrossOrigin annotation. By default the @ CrossOrigin annotation allows all origin, all headers, the HTTP methods specified in the @ RequestMapping annotation and maxAge of 30 min. You can customize the behavior by specifying the corresponding attribute values.
Composed Annotations (@ RequestMapping Variants)
Using these annotations have become the standard ways of defining the endpoints. They act as wrapper to @ RequestMapping.
1. @ GetMapping
This annotation is used for mapping HTTP GET requests onto specific handler methods. @ GetMapping is a composed annotation that acts as a shortcut for @ RequestMapping(method = RequestMethod.GET)
2. @ PostMapping
This annotation is used for mapping HTTP POST requests onto specific handler methods. @ PostMapping is a composed annotation that acts as a shortcut for @ RequestMapping(method = RequestMethod.POST)
3. @ PutMapping
This annotation is used for mapping HTTP PUT requests onto specific handler methods. @ PutMapping is a composed annotation that acts as a shortcut for @ RequestMapping(method = RequestMethod.PUT)
4. @ PatchMapping
This annotation is used for mapping HTTP PATCH requests onto specific handler methods. @ PatchMapping is a composed annotation that acts as a shortcut for @ RequestMapping(method = RequestMethod.PATCH)
5. @ DeleteMapping
This annotation is used for mapping HTTP DELETE requests onto specific handler methods. @ DeleteMapping is a composed annotation that acts as a shortcut for @ RequestMapping(method = RequestMethod.DELETE)
6. @ ExceptionHandler
This annotation is used at method levels to handle exception at the controller level. The @ ExceptionHandler annotation is used to define the class of exception it will catch. You can use this annotation on methods that should be invoked to handle an exception. The @ ExceptionHandler values can be set to an array of Exception types. If an exception is thrown that matches one of the types in the list, then the method annotated with matching @ ExceptionHandler will be invoked.
7. @ PathVariable
This annotation is used to annotate request handler method arguments. The @ RequestMapping annotation can be used to handle dynamic changes in the URI where certain URI value acts as a parameter. You can specify this parameter using a regular expression. The @ PathVariable annotation can be used declare this parameter.
8. @ RequestBody
This annotation is used to annotate request handler method arguments. The @ RequestBody annotation indicates that a method parameter should be bound to the value of the HTTP request body. The HttpMessageConveter is responsible for converting from the HTTP request message to object.
9. @ RequestParam
This annotation is used to annotate request handler method arguments. Sometimes you get the parameters in the request URL, mostly in GET requests. In that case, along with the @ RequestMapping annotation you can use the @ RequestParam annotation to retrieve the URL parameter and map it to the method argument. The @ RequestParam annotation is used to bind request parameters to a method parameter in your controller.
10. @ ResponseBody
This annotation is used to annotate request handler methods. The @ ResponseBody annotation is similar to the @ RequestBody annotation. The @ ResponseBody annotation indicates that the result type should be written straight in the response body in whatever format you specify like JSON or XML. Spring converts the returned object into a response body by using the HttpMessageConveter.
11. @ ResponseStatus
This annotation is used on methods and exception classes. @ ResponseStatus marks a method or exception class with a status code and a reason that must be returned. When the handler method is invoked the status code is set to the HTTP response which overrides the status information provided by any other means. A controller class can also be annotated with @ ResponseStatus which is then inherited by all @ RequestMapping methods.
12. @ ControllerAdvice
This annotation is applied at the class level. As explained earlier, for each controller you can use @ ExceptionHandler on a method that will be called when a given exception occurs. But this handles only those exception that occur within the controller in which it is defined. To overcome this problem you can now use the @ ControllerAdvice annotation. This annotation is used to define @ ExceptionHandler, @ InitBinder and @ ModelAttribute methods that apply to all @ RequestMapping methods. Thus if you define the @ ExceptionHandler annotation on a method in @ ControllerAdvice class, it will be applied to all the controllers.
13. @ RestController
This annotation is used at the class level. The @ RestController annotation marks the class as a controller where every method returns a domain object instead of a view. By annotating a class with this annotation you no longer need to add @ ResponseBody to all the RequestMapping method. It means that you no more use view-resolvers or send html in response. You just send the domain object as HTTP response in the format that is understood by the consumers like JSON.
14. @ RestControllerAdvice
This annotation is applied on Java classes. @ RestControllerAdvice is a convenience annotation which combines @ ControllerAdvice and @ ResponseBody. This annotation is used along with the @ ExceptionHandler annotation to handle exceptions that occur within the controller.
Spring Boot Annotations
1. @ EnableAutoConfiguration
This annotation is usually placed on the main application class. The @ EnableAutoConfiguration annotation implicitly defines a base “search package”. This annotation tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
2. @ SpringBootApplication
This annotation is used on the application class while setting up a Spring Boot project. The class that is annotated with the @ SpringBootApplication must be kept in the base package. The one thing that the @ SpringBootApplication does is a component scan. But it will scan only its sub-packages. As an example, if you put the class annotated with @ SpringBootApplication in com.example then @ SpringBootApplication will scan all its sub-packages, such as com.example.a, com.example.b, and com.example.a.x.
The @ SpringBootApplication is a convenient annotation that adds all the following:
- @ Configuration
- @ EnableAutoConfiguration
- @ ComponentScan
Spring Testing Annotations
1. @ BootstrapWith
This annotation is a class level annotation. The @ BootstrapWith annotation is used to configure how the Spring TestContext Framework is bootstrapped. This annotation is used as a metadata to create custom composed annotations and reduce the configuration duplication in a test suite.
2. @ ContextConfiguration
This annotation is a class level annotation that defines a metadata used to determine which configuration files to use to the load the ApplicationContext for your test. More specifically @ ContextConfiguration declares the annotated classes that will be used to load the context. You can also tell Spring where to locate for the file. @ ContextConfiguration(locations={"example/test-context.xml", loader = Custom ContextLoader.class})
3. @ WebAppConfiguration
This annotation is a class level annotation. The @ WebAppConfiguration is used to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext. This annotation is used to create the web version of the application context. It is important to note that this annotation must be used with the @ ContextConfiguration annotation.The default path to the root of the web application is src/main/webapp. You can override it by passing a different path to the @ WebAppConfiguration
.
4. @ SpringBootTest
This annotation is used to start the Spring context for integration tests. This will bring up the full autoconfigruation context.
5. @ DataJpaTest
The @ DataJpaTest annotation will only provide the autoconfiguration required to test Spring Data JPA using an in-memory database such as H2. This annotation is used instead of @ SpringBootTest
6. @ DataMongoTest
The @ DataMongoTest will provide a minimal autoconfiguration and an embedded MongoDB for running integration tests with Spring Data MongoDB.
7. @ WebMVCTest
The @ WebMVCTest will bring up a mock servlet context for testing the MVC layer. Services and components are not loaded into the context. To provide these dependencies for testing, the @ MockBean annotation is typically used.
8. @ AutoConfigureMockMVC
The @ AutoConfigureMockMVC annotation works very similar to the @ WebMVCTest annotation, but the full Spring Boot context is started.
9. @ MockBean
Creates and injects a Mockito Mock for the given dependency.
10. @ JsonTest
Will limit the auto configuration of Spring Boot to components relevant to processing JSON. This annotation will also autoconfigure an instance of JacksonTester or GsonTester.
11. @ TestPropertySource
Class level annotation used to specify property sources for the test class.
Conclusion:
Spring annotations play a crucial role in simplifying the configuration and development of Spring applications. This article has covered some of the most commonly used Spring annotations, but there are many more available in the Spring ecosystem to cater to various needs. Understanding and effectively utilizing these annotations can significantly enhance your productivity and the maintainability of your Spring-based projects.