Understanding Spring Annotations: A Comprehensive Overview
Willian Ferreira Moya
Posted on June 5, 2024
Introduction
Working with Spring implies using lots of annotations to configure your application, link components, and manage behaviors. These annotations can be separated into some categories: Initialization annotations, Configuration Specifics annotations, Stereotypes, Behavioral, and Testing.
This overview aims to help you discover some new annotations or to understand briefly what some annotations that you may be seeing in your project do, so you can better understand a little bit what is going on and why they are there. It can help you have some ideas to apply some of those in your project.
In this first article of the series, we will cover an overview of each annotation and briefly explain what each one does. In the following articles, I’ll show you how to use them and some specific use cases for each one.
Initialization Annotations
@Value
This annotation is usually used to inject values from your configuration properties.
@Required
It has the function to mark a dependency as mandatory but it became deprecated.
@Autowired
It allows Spring to link beans from the Spring IoC container.
@lazy
It makes a bean to be late initiated, and not initialized with the application context, but when it is going to be used.
@AliasFor
It allows you to create different names for the same attribute.
@Qualifier
It allows you to define which bean to use when you have two beans with the same type.
@primary
It defines which of the beans with the same type is the default.
@DependsOn
It defines beans that the current beans depend on. It guarantees that all the dependencies will be created first.
@Secured
This annotation specifies which user role (it can be more than one) has access to a method.
@resource
It shows Spring to inject a bean based on its name in a method.
@PreDestroy
Spring will call the method where this annotation is set before a bean is removed from the context.
@PostConstruct
Spring will call the method with this annotation to execute after the bean is created.
@Lookup
It will get the return type of the method to replace its behavior, returning a bean of the same type.
Configuration Specific Annotations
@Import
Indicates one or more components to import, usually classes with @Configuration in it.
@profile
When combined with a bean, it will only make the bean available if the Spring active profile is set to the same value as defined in @profile(”prod”).
@ComponentScan
Used with @Configuration, we can use this annotation to point to Spring where they are located.
@bean
This marks to Spring that the return type of the method is an instance to be created, managed, and injected by the Spring IoC container.
@PropertySource
An annotation used in conjunction with @Configuration to add property sources to the application environment.
@scope
This annotation is used to define the lifetime scope of a bean.
Stereotypes
@Component
This annotation tells Spring that our class needs to be created, managed, and injected as an application bean.
@Configuration
This annotation indicates for Spring that the class is a source of bean definitions.
@Controller
This annotation is a specialization of @Component and has the objective of handling web requests.
@RestController
This annotation is a combination of @Controller with @ResponseBody to make it easier to create RESTful web services.
@Service
This is a specialization of the @Component that aims to hold business logic in it.
@Repository
This annotation is for classes that deal with data persistence.
Behavioral Annotations
@Aspect
This annotation tells Spring that this class is an aspect, meaning this class contains code specific to a cross-cutting concern.
@Transactional
Spring will create a transaction wrapping the class or method and manage its lifecycle.
Test Annotations
@BootstrapWith
You can use this annotation to tell how Spring TestContext should be bootstrapped (you could use this to load only specific configurations to your context).
@ContextConfiguration
This annotation is used to tell how to load and configure the application context for your tests.
@WebAppConfiguration
It loads a web application context in testing.
@ContextHierarchy
It defines the hierarchy of the configurations when configuring the test application context.
@ActiveProfiles
It defines which profile will be used in your test application context.
@TestPropertySource
It helps you to configure the property source that Spring will use in your tests.
@DynamicPropertySource
This allows you to set a method to add dynamic properties to your property source.
@DirtiesContext
It tells Spring to restart the context in testing. Spring by default reuses the context in testing; this will force it to restart.
@TestExecutionListeners
Similar to JUnit, Spring provides some listeners that will execute actions that can be before, after, etc. (just like JUnit @Before).
@RecordApplicationEvents
This annotation will record all the application events published in the application context during a test execution.
@commit
This will tell the transaction to commit after the execution of a test method.
@Rollback
This annotation will make the transaction rollback after the test method execution.
@BeforeTransaction
Defines a behavior that will run before starting a transaction.
@AfterTransaction
Defines a behavior that will happen after the transaction ends.
@sql
This will run SQL in the database when running an integration test.
@SqlGroup
Creates a group of @sql annotations to run when running your integration tests.
Conclusion
In the upcoming articles, we will explain tips, tricks, and use cases for each of these annotations. Stay tuned for detailed guides on leveraging these annotations to enhance your Spring applications.
Follow me!
Posted on June 5, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.