If you’re here, it’s probably because you have never really understood the differences between those Spring annotations:
- What is the Spring
- What’s the key difference between a class annotated with
- How can I use
The bad news is Your search is over! It’s finally time to get a better understanding of when and how to use those annotations.
The section above describe each annotation and the best way to use them to fully leverage the power of Spring Framework.
All those annotations are designed to:
- annotate classes,
- enable instantiation and autowiring of those annotated classes.
But, how do they differ from each other? Let’s dive into Spring’s internals to find out!
@Configuration annotation is used to mark a class as being a Java Spring Config. Here is a simple example:
Spring Configurations aim only to provide bean instances. Configuration are usually discovered through component-scanning or Spring XML Configuration.
@Bean annotation is exclusively reserved to be used to annotate a method of a
@Configuration annotated class. This annotation indicates the method is returning a bean that must be registered within the application context.
@Autowired annotation is used to mark a constructor, a method or a field as eligible for Spring autowiring. Basically, autowiring is the process of injecting an instance of a wanted class or interface into another object. Let’s see a real-world example:
In the example above, the
DriverProvisioningService class is an implementation of
DockerCloudInstanceService. The class is annotated with
@Service, telling Spring this is a service. We’ll see later what this means.
The constructor is annotated with
@Autowired to tell Spring to instantiate the
DriverProvisioningService with this constructor, and provide instances for the specified constructor parameters.
Please note that
@Autowired is only required when:
- multiple constructors are defined,
- injecting a bean directly in a field,
- or injecting a bean through a setter.
Most of the time, when performing constructor based bean injection, the
@Autowired annotation can be ignored.
PostContruct and PreDestroy
@PostContruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. Here is an example:
The method annotated by
@PostConstruct must fulfill the following criterias:
- must not have any parameter (except
InvocationContextwhen annotating a method on a bean interceptor)
- must have package or public visibility,
- it can throw checked exceptions,
postContruct method allows to perform actions once all the beans have been instantiated and autowired. It’s a good way to execute some code on application startup.
Predestroy works exactly the same way, but enables to execute some arbitrary code before the application is shutdown.
@Resource annotation marks a resource that is needed by the application. It’s the equivalent of
@Autowired from Spring, but
@Resource is part of the JSR-250 like
@Resource annotation has the following execution paths: (by order)
- Match by Name,
- Match by Type,
- Match by Qualifier.
On the first match, the bean matching the configuration is injected.
Suppose we have a Spring configuration as following:
Bean annotations are used to tell Spring to instantiate a given class and register it into the application context.
@Scope annotation defines the scope of the instance of the bean being marked with. By default, Spring creates singleton for each bean. But, some beans can be session scoped, or even request scoped in web applications.
Here are the known scopes:
- singleton: there is only one instance of the bean per Spring context,
- prototype: there is one instance of the bean per injection in any other bean, or each time it’s requested,
- request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext,
- session: the bean’s scope is narrowed to the HTTP session,
- global-session: This scopes a bean definition to a global HTTP session.
99% of beans are usually singleton scoped. Let’s see an example:
In singleton scope, both injected person beans are the same instance. In prototype scope, each person instance will be unique.
@Component is truely the center piece of Spring autowiring mechanisms:
Added as of Spring 2.5, the
@Component annotation is used to annotate a type (or class).
@Component is the most general Spring Autowiring annotation. You can use it to annotate general purpose classes with no special semantic (unlike Database Repositories, long-life services etc.) like the following:
The class above is an event-bus listener. It listens to events being posted on an
EventBus and subscribe to one of those (
You can use
@Component for pretty much anything that needs to be instantiated and autowired by Spring. For more specialized beans, the next coming annotations are best suited.
@Repository annotation is used to mark a DAO (Data Access Object) bean. Repositories are popular DAOs.
Here is an example using Spring Data JPA:
In this case, the Repository instance is generated by Spring. Spring uses Reflection as well as Java Proxy to generate the bytecode of an implementation on runtime. You can also create your own implementation (see Spring Data for more information).
For this reason, repositories are a special kind of beans.
@Service annotation is a specialization of the
@Component annotation. In an MVC Pattern, there are three components: (a simplified explanation)
- Model: represents the services to create and manipulate data,
- View: represents the view layer, which is the UI representation of the data,
- Controller: the controller acts as a middle-ware between the Model and the View, orchestrating everything in-between.
@Service annotated beans are part of the Model within the MVC architecture.
Here is an example of service:
The service here provides an abstraction layer between the business code and the lower-level repository code. It abstracts the queries to perform on the database by hiding them behind simple methods.
Services should be long-living instances, usually living as long as the application, and mostly be stateless. A service is expected to behave exactly the same way no matter what the previous calls were.
We’re reviewed a few of the annotations involved when using Spring Framework. Most of them are pretty simple to understand! We hope this little article gave you a more insight about which annotations are available, and how to use them.
Keep in mind the KISS principle: keep things simple, don’t overuse advanced features. Most of the time
@Service annotated beans are fine. Most spring configuration can be done with Java using
@Configuration annotated classes and