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.
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
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.
Spring auto-detected generic component. In our project we used only one time. We prefer to use specialized components (Controller, Repository)
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.
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.
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)
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.
It allows the scan of the @ConfigurationProperties annotation.
It is a specialization of @Component (autodetected during the startup scanning). The Repository class contains methods to store and retrieve data in a collection of objects (e.g. database). It represents the DAO in Java EE.
It’s an annotation that includes @Controller and @ResponseBody and it is used to declare web controller that maps http requests with Spring functions.
It maps a path in the URL request with a method declared in Spring. In the project we used the specializations @GetMapping, @PostMapping etc.
@Service @Value @Scheduled
@EnableIntegration @IntegrationComponentScan @EnableIntegrationManagement @EnableIntegrationMBeanExport @Splitter
@ActiveProfiles @ContextConfiguration @MockBean @Qualifier @TestPropertySource