Spring MVC annotations in detail (SPRINGMVC mybatis Spring)

Source: Internet
Author: User


1. Declaring the Bean's annotations:
1.1 @Component: Component, no explicit role
1.2 @Service: Using at the Business Logic Layer (service tier)
1.3 @Repository: Using the Data Access Layer (DAO layer)
1.4 @Controller: Used in the presentation layer (MVC--SPRINGMVC)

2. Notes injected into the bean:
2.1 @Aautowired: Annotations provided by spring
The class member variables, methods and constructors are labeled to complete the work of automatic assembly. Use of @Autowired to eliminate set, get method
By default assembly by type (this annotation is industry spring), by default must require that the dependent object must exist, if you want to allow null value, you can set its Required property to False,
such as: @Autowired (required=false), if we want to use name assembly can be used in conjunction with @qualifier annotations

2.2 @Inject: Annotations provided by JSR-330 (Java's Dependency injection standard)
The same as @aautowired.

2.3 @Resource: Annotations provided by JSR-250
The default name of the photo is assembled, and the name can be specified by the Name property,
If the Name property is not specified, when the annotation is written on a field, the default fetch field name is searched by name, and if the annotation is written on the setter method, the property name is assembled by default. When a bean matching the name cannot be found, it is assembled by type. However, it is important to note that if the Name property is specified once, it will only be assembled by name.

2.4 The difference between three
@Autowired and @inject are all dependency injections implemented through the Autowiredannotationbeanpostprocessor class, which are interchangeable.
@Resource implementing Dependency Injection through the Commonannotationbeanpostprocessor class
@Autowired and @Inject
Matches by Type
Restricts by Qualifiers
Matches by Name
@Resource
Matches by Name
Matches by Type
Restricts by Qualifiers (ignored if match was found by name)

3. Annotations to the configuration file:
3.1 @Configuration: Declares that the current class is a configuration class, equivalent to a spring-configured XML file.
3.2 @ComponentScan (Cn.test.demo): Automatically scans all classes that use @Component @Service @Repository @Controller under the package and registers as a bean
3.3 @WiselyConfiguration: Combination annotations can replace @Configuration and @componentscan
3.4 @Bean: Annotations on a method, declares that the return value of the current method is a Bean.
3.5 @Bean (initmethod= "AA", destroymethod= "BB")--Specify the AA and BB methods after construction. Executed before the bean is destroyed.

4. AOP programming Annotations:
@Aspect: Declares that this is a facet
@After @Before. @Around define facets, you can directly use the Intercept rule (pointcut PointCut) as the parameter
@PointCut: Specifically, the interception rules are defined and then @Before @After. @Around call
@Transcational: Transaction
@Cacheable: Data cache
@EnableAaspectJAutoProxy: Turn on spring support for this aspect (Aspect)
@ Target (elementtype.type): Meta annotation, which specifies the member of the callout adornment class--Specify the Intercept rule
@Retention (retentionpolicy.runtime)---> When the @retention of the defined annotations is runtime, it is possible to process annotations .--> by the reflection mechanism of the runtime to specify the interception rule

5. @Enable * Notes:
@EnableAsync: Turn on support for asynchronous tasks (multithreading)
@EnableScheduling: Turn on support for scheduled tasks (timers)
@EnableWebMVC: Turn on configuration support for Web MVC
@EnableAaspectJAutoProxy: Turn on spring support for this facet (Aspect)
@EnableConfigurationProperties turn on support for @configurationproperties annotation configuration bean
@EnableJpaRepositories: Open support for spring Data JAP Repository
@EnableTransactionManagement open support for annotated things
@EnableCaching open annotations are supported for caching.
@EnableDiscoveryClient Let the service discover the server, using the server. Spring Cloud realizes service discovery
@EnableEurekaServer Registration Server Spring Cloud implements service registration @
@EnableScheduling allows spring to schedule tasks similar to namespaces in the Spring.xml file <task:*>
@EnableCaching open cache cache support;

6. Springmvc commonly used annotations:
@Controller: Annotations Declare this class as a Controller in SPRINGMVC and declare it as a spring bean.
@RequestMapping: You can annotate Web requests (access paths and parameters) on classes and methods
@RequestMapping (value= "/convert", produces+{"application/x-wisely"}) set the Access URL return value type
@ResponseBody: Supports putting the return value into response instead of returning a page (a group of data is returned)
@RequestBody: The request parameter is allowed in the request body instead of directly attached to the address after the annotation is placed before the parameter
@Path Variable: Used to receive path parameters such as/test/001,001 as parameters, the second note is placed before the parameter
@RestController: @Controller + @ResponseBody combination annotations
@ControllerAdvice: The global configuration of the controller can be placed in the same location via @controlleradvice
@ExceptionHandler: Exception for global processing controller
@ExceptionHandier (Value=exception.class)--through the Value property to filter the interceptor condition to intercept all exceptions
@InitBinder: Used to set Webdatabinder, Webdatabinder is used to automatically bind the foreground request parameters to the model.
@ModelAttrbuute: Binding key-value pairs into model,
@RunWith: Runner
@RunWith (junit4.class) means to run with JUNIT4
@RunWith (Springjunit4classrunner.class), let the test run in the spring test environment
@RunWith (Suite.class) is a set of tests,
@WebAppConfiguration ("Src/main/resources"): Annotations on classes that declare the loaded Applicationcontex to be a webapplicationcontext, Its properties specify the location of the Web resource, the default is Src/main/webapp, and the customization is modified to resource
@Before: Initialize before xxx

Spring MVC annotations in detail (SPRINGMVC mybatis Spring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.