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 | for SpringMVC related annotation.

The Spring part

1. 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)

Want to learn more
JavaFor architecture technology, you can follow me. I will also sort out more knowledge points about architecture technology and share some of them: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, microservice architecture principle, JVM performance optimization, concurrent programming these become the architect essential knowledge system.

More communication and access methods can be scanned



2. Inject annotations for 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. Annotations related to Java configuration classes

@Configuration declares the current class to be a Configuration class, equivalent to the Spring Configuration in XML form (class-wise)

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 related annotations

Spring supports AspectJ’s annotated aspect programming.

@aspect Declares an Aspect (on class)

Advice is defined with @after, @before, and @around, taking the interception rule (pointcut) directly as an argument.

@after executes After method execution (method)

@before executed Before method execution (method)

@around Before and after method execution (method)

@pointcut declares pointcuts

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

Attribute support for 5.@Bean

@scope sets up how the Spring container will create Bean instances (by method, @bean is required)

The setting types include:

Singleton (Singleton, one bean instance in a Spring container, default mode),

Protetype (create a new bean per call),

Request (in a Web project, create a new bean for each HTTP Request),

Session (in a Web project, create a new bean for each HTTP Session),

GlobalSession (Create a new 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

6. @ Value annotation

@value injects values into properties (on properties)

The following injection modes are supported:

“Injects ordinary characters

@Value("Michael Jackson")
String name;
Copy the code

Inject operating system properties

@Value("#{systemProperties['os.name']}")
String osName;
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/hgs/hello/test.txt")
String Resource file;
Copy the code

Inject website resources

@Value("http://www.cznovel.com") Resource url; 12Copy the code

Injects the configuration file

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

Injection configuration usage:

(test.properties)

Book.name = The Three-Body ProblemCopy the code

② @propertysource Load configuration file (class)

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

(3) also need to configure a PropertySourcesPlaceholderConfigurer bean.

7. Switch the environment

@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

@ 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)1
Copy the code

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

@ContextConfiguration(classes={TestConfig.class})1
Copy the code

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, Class methods annotated with @Controller can be annotated with @ExceptionHandler, @initBinder, and @ModelAttribute.

This is valid for all methods in the controller annotated @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.

If there are omissions or mistakes, I hope to help point out.


Want to learn more
JavaFor architecture technology, you can follow me. I will also sort out more knowledge points about architecture technology and share some of them: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, microservice architecture principle, JVM performance optimization, concurrent programming these become the architect essential knowledge system.

More communication and access methods can be scanned