Annotations themselves are not functional, just like XML. Annotations and XML are a type of metadata, and metadata is the data that interprets the data, known as configuration.

This paper lists the Spring | Spring MVC related annotation.

The Spring part

Declare the bean’s annotations

The @Component Component has no explicit role

@service is used at the business logic layer (Service layer)

@repository is used in the data access layer (DAO layer)

Controller used in presentation layer, Controller declaration (C)

Inject annotations to the bean

Autowired: Provided by Spring

Inject: Provided by JSR-330

@Resource: Provided by JSR-250

Can be annotated on set methods and properties. It is recommended to annotate on properties.

3. Java configuration class annotations

On a method, declare the return value of the current method as a Bean, instead of the method in XML (on a method).

@Configuration declares the current class to bea Configuration class with the @Component annotation inside to indicate that the class is a bean (on the class)

@ComponentScan is used to scan components, equivalent to (classically) in XML

@WishlyConfiguration is a combination of @Configuration and @ComponentScan. It can replace the two annotations

4. AOP annotations

Spring supports AspectJ’s annotated aspect programming.

@aspect declares an Aspect (classically) that defines advice using @after, @before, and @around, taking the interception rule (pointcut) directly as an argument.

@after is executed After method execution (method) @before is executed Before method execution (method) @around is executed Before and After method execution (method)

Enable Spring support for AspectJ proxies in Java configuration classes using the @enableAspectJAutoProxy annotation (class-level)

Attribute support for @beans

@scope sets how the Spring container will create new Bean instances (or, by definition, @beans).

Singleton (Singleton, one bean instance in a Spring container, default mode), Protetype (new bean per call), Request (in web projects, Create a bean for each HTTP Request), Session (create a bean for each HTTP Session in a Web project), GlobalSession (create a bean instance for each Global HTTP Session)

@stepScope is also covered in Spring Batch

@postconstruct is provided by JSR-250 and is executed after the constructor is finished, equivalent to initMethod of beans in XML configuration files

The @predeStory is provided by JSR-250 and is executed before the Bean is destroyed, equivalent to the destroyMethod of beans in an XML configuration file

@value Can be used to inject a Value into an attribute (on an attribute)

@Value("Yi Hu")
String name;
Copy the code

Inject operating system properties

@Value("#{systemProperties['os.name']}")
String name;
Copy the code

Injection expression result

@Value("#{T(java.lang.Math).random()*100}")
String randomNumber;
Copy the code

Inject other bean properties

@Value("#{domeClass.name}")
String name;
Copy the code

To inject file resources

@Value("classpath:com/test.txt")
String Resource file;
Copy the code

Inject website resources

@Value("https://www.baiducom")
Resource url;
Copy the code

Injects the configuration file

@Value("${book.name}")
String bookName;
Copy the code

Injection configuration usage:

(test.properties)

Book.name = The Three-Body Problem

② @propertysource Load configuration file (class)

@PropertySource("classpath:com/test.propertie")
Copy the code

(3) also need to configure a PropertySourcesPlaceholderConfigurer bean.

7. Environment switch

@profile Sets the configuration Environment that the current context needs to use by setting the ActiveProfiles of the Environment. (class or method)

This annotation can be used in @Conditional Spring4 to define a Conditional bean, which can be instantiated by implementing the Condition interface and overriding the matches method. (Methodological)

8. Asynchronous correlation

@enableAsync Configuration class with this annotation to enable support for asynchronous tasks, narrative AsyncConfigurer interface (class on)

@async The bean method that actually executes uses this annotation to declare that it is an asynchronous task. (All methods on a method or class will be asynchronous, requiring @enableAsync to start the asynchronous task.)

9. Related to scheduled tasks

EnableScheduling for use on configuration classes to enable support for scheduled tasks (on class)

Scheduled @scheduled is a Scheduled task, including cron,fixDelay,fixRate, etc.

10. @enable * Notes

These comments are mainly used to turn on support for XXX. @enableAspectJAutoProxy Enables automatic AspectJ proxy support

@enableAsync Enables support for asynchronous methods

@enablescheduling Enable scheduled task support

@enableWebMVC Enables Web MVC configuration support

@ EnableConfigurationProperties open support for @ ConfigurationProperties annotation configuration Bean

@EnableJpaRepositories Enable support for SpringData JPA Repository

@ EnableTransactionManagement open annotation type transaction support

@enablecaching Enables annotated caching support

11. Test relevant notes

The @runwith runner, commonly used in Spring to support JUnit

@RunWith(SpringJUnit4ClassRunner.class)

The @contextConfiguration is used to load the configuration ApplicationContext, where the classes property is used to load the configuration class

@ContextConfiguration(class={TestConfig.class})

For SpringMVC part

EnableWebMvc enables Web MVC configuration support in a configuration class such as some ViewResolver or MessageConverter. If not, override the WebMvcConfigurerAdapter method (for SpringMVC configuration).

@Controller declares this class to be a Controller in SpringMVC

RequestMapping is used to map Web requests, including access paths and parameters (class or method)

@responseBody Supports putting a return value inside a response instead of a page, and usually the user returns JSON data (next to the return value or on the method).

RequestBody @requestBody allows the parameters of a request to be in the body of the request, instead of being attached directly to the address. (Placed before parameters)

@pathvariable is used to receive path parameters, such as the path declared by @requestMapping (” /hello/{name} “). You can obtain the value after putting the annotation in the parameter. It is usually implemented as a Restful interface method.

@restController this annotation is a combined annotation, which is the equivalent of @Controller and @ResponseBody, annotation on the class, which means that all of the methods of this Controller are going to have @ResponseBody by default.

@ControllerAdvice With this annotation, we can put the global configuration for the controller in one place, Methods on classes annotated with @Controller can be annotated with @ExceptionHandler, @initBinder, and @ModelAttribute. This applies to all methods in controllers annotated with @requestMapping.

ExceptionHandler Is used to handle exceptions in the controller globally

InitBinder is used to set up WebDataBinder, which is used to automatically bind foreground request parameters to the Model.

@ModelAttribute is used to bind key/value pairs to Model. @ControllerAdvice allows @requestMapping globally to get the key/value pairs set here.

With great power comes great responsibility