Spring common annotations meaning

Updated:

During the review of a recent project I listed the Spring annotations used during the development.

Good Java developers without Spring experience could have difficulties with all this annotations.
For this reason the list want to help them to quickly understand what's the goal of the annotation and a link to the documentation.

Some are commonly used in every project, a few are less known. I will add some examples, for the moment you can find the link to the documentation.

Spring annotations

@Autowired

This is one of the must (mis)used annotation of Spring for injecting Beans.
In our team we decided to don't allow the use of this annotation in our code with the exception of the Test classes.
We injected beans using the constructor as recommended by Spring.
Here the explanation about our decision: Spring best practices

@ActiveProfiles

This annotation is used to explicitly set the active profiles for a test class or a test method when running unit tests.

@Bean

Method annotation, the returned value is registered as a bean within a BeanFactory. A Beans is (by default) a single instance of an class.
In our project we used a lot of @Bean instances to configure the connection to external systems.

@Component

Spring auto-detected generic component. In our project we used only one time.
This annotation marks a Java class as a Spring component. Spring will automatically detect and instantiate these components during application startup. It is a generic stereotype for any Spring-managed component.
We prefer to use specialized components (Controller, Repository)

@ConditionalOnProfile

This annotation is used to conditionally enable or disable a bean based on the active profile(s).
It works in conjunction with the `@Profile annotation.

@ConditionalOnProperty

This annotation is helpful when different beans have to deployed in different environments (integration tests / local development / production). Using a property file we can enable or disable the instantiation of the beans at the startup. I liked this annotation a lot.

@Configuration

Indicates that a class declare one or more @Bean methods.
At the start of the application Spring reads the `@Configuration classes and registers the declared beans.

@ConfigurationProperties

With @ConfigurationProperties we replaced long lists of @Value fields in configuration files with only one import of a property object.
This annotation is helpful when you have to work with a lot of external parameters (e.g. property files)

@ComponentScan

This annotation tells to Spring that some packages have to be scanned searching for annotated beans. Without this annotation your @Bean, @Controller etc. don't have any effect.

@Controller

This annotation is used to mark a class as a Spring MVC controller.
It is responsible for handling and processing HTTP requests from clients.
Controllers typically contain methods annotated with @RequestMapping to map specific URLs.

@EnableConfigurationProperties

It allows the scan of the @ConfigurationProperties annotation.

@Profile

This annotation is applied at the class or method level to indicate that a bean or a configuration class should be active only when the specified profile(s) are active.

You can provide one or multiple profile names as arguments to the annotation.

@ProfileValueSourceConfiguration

This annotation is used to define a custom profile value source for resolving expressions within @Profile annotations.

It is typically used in conjunction with `@Profile to provide dynamic profile resolution based on external configuration sources.

@Repository

This annotation is used to indicate that a class serves as a repository or data access object (DAO). It typically interacts with a database or other external data sources.

The annotation enables exception translation for the annotated class, converting database-specific exceptions into Spring's DataAccessException hierarchy.

@RestController

It's an annotation that includes @Controller and @ResponseBody and it is used to declare web controller that maps http requests with Spring functions.

@RequestMapping

This annotation maps HTTP requests to specific methods in a controller class.

It defines the URL pattern and HTTP method type for which the annotated method should be invoked.

In the project we used the specializations @GetMapping, @PostMapping etc.

@RestController

This annotation combines the functionality of @Controller and @ResponseBody.

It is used to create RESTful web services and is typically used when the controller methods return data instead of rendering views. The return values are automatically serialized to JSON or XML.

@Service

This annotation marks a class as a service component in the business layer.
It represents the service layer in the application and encapsulates the application's business logic.

Service classes often contain transactional operations and are typically used by controllers.

@Transactional

This annotation is used to mark a method or class as transactional.
It ensures that the annotated method or all methods within the annotated class are executed within a transactional context.

It is commonly used in the service layer to provide transaction management for database operations.

@Scheduled

It is used to schedule the execution of methods at fixed rates or intervals.

It is commonly used for tasks such as batch processing, data synchronization, or sending periodic notifications.


WebApp built by Marco using SpringBoot 3.2.4 and Java 21, in a Server in Switzerland without 'Cloud'.