Spring and SpringMVC common annotations (rpm)

Author: IT_faquir 
Original: https: //blog.csdn.net/IT_faquir/article/details/78025203 

Personal learning used, if infringement, please contact deleted!

--------------------- 

This article lists Spring | Profile SpringMVC related annotations.
Spring section
1. Declare bean notes

@Component components, there is no clear role

@Service use (service layer) Yewuluojiceng

Use @Repository (DAO layer) in the data access layer

In use @Controller presentation layer, the statement of the controller (C)

2. Fill the bean notes

@Autowired: provided by Spring

@Inject: JSR-330 by

@Resource: JSR-250 by

Can comment on a set of methods and properties, we recommended annotation on the property (at a glance, write less code).

About ambiguity problem @Autowired can see @Autowired Depth - the three ways, a variety of ambiguity solution
@Qualifier and @Primary can be used in the ambiguity of the issue bean

3.java configuration class-related notes

Spring configuration @Configuration current class is declared class configuration, xml equivalent of (upper class)

@Bean annotations on the method, the method returns the current value of the statement of a bean, the alternative embodiment xml (the Method)

@Configuration declare the current class to class configuration, which combines internal @Component comments, indicating that this class is a bean (the class)

Component @ComponentScan for scanning, corresponding to the xml context: (upper class) component-scan /

@WishlyConfiguration combination with @ComponentScan @Configuration annotations can replace the two annotations

4. section (AOP) associated annotations

Spring supports AspectJ annotation style section of programming.

@Aspect declare a section (upper class)
using @ After, @ Before, @ Around defined suggestions (advice), can be directly blocking rules (cutting point) as a parameter.

@After performed (the method) after performing the method
@Before performed (the method) before the method is performed
@Around performed after and before performing the method (the method)

@PointCut sound Mingqie point
using @EnableAspectJAutoProxy Spring's support for AspectJ annotation to open the agent (the class) in java class configuration

5. @ Bean attributes support

How to create a new container is provided @Scope Spring Bean instance (the method, must have @Bean)
provided type comprising:

Singleton (single embodiment, a container is only one example of bean Spring, default mode),
Protetype (each time a new call bean),
the Request (Web project, a new http request for each bean),
the Session (Web project, each http session to create a new bean),
globalSession (to each create a new global http session bean instance)

@StepScope also involved in the Spring Batch

@PostConstruct by JSR-250, executed after executing the constructor, equivalent to the xml configuration file in the bean initMethod

@PreDestory by JSR-250, before performing the destruction Bean, equivalent to the bean xml configuration file destroyMethod

6. @ Value Notes

@Value injection attribute value (the property)
to support the injection manner:
"ordinary characters injection

@Value ( "Michael Jackson")
String name;
1
2
"injection operating system properties

@Value ( "# {systemProperties [ 'The os.name']}")
String OSNAME;
. 1
2
"injection Expression Results

@Value ( "# {T (java.lang.Math) .random ()} * 100")
String randomNumber;
. 1
2
"Other injection bean property

@Value ( "domeClass.name # {}")
String name;
. 1
2
"injection Resource File

@Value ( "the CLASSPATH: COM / HGS / the Hello / test.txt")
String Resource File;
1
2
"injection site resources

@Value ( "http://www.cznovel.com")
the Resource URL;
. 1
2
"injection profile

@Value ( "book.name $ {}")
String bookName;
. 1
2
injection configuration using the method:
① write configuration file (, test.properties)

book.name = "three body"
. 1
② @PropertySource load configuration file (the class)

@PropertySource ( "CLASSPATH: COM / HGS / Hello / Test / test.propertie")
. 1
③ PropertySourcesPlaceholderConfigurer the need to configure a bean.

7. context switching

@Profile to set the current configuration environment context to use by setting Environment of ActiveProfiles. (Classes or methods)

@Conditional Spring4 can be used in this condition, then the annotation defines bean, by implementing the Condition interface and override matches method, to determine whether the bean is instantiated. (Methods)

8. asynchronous related

@EnableAsync configuration class, this comment by open support for asynchronous tasks (upper class) narrative AsyncConfigurer Interface

@Async use the annotation bean method to declare its actual implementation is an asynchronous task (all the way on the method or the class will be asynchronous, asynchronous tasks require @EnableAsync open)

9. The timing of tasks related to

@EnableScheduling class configuration on the use of open support for scheduled tasks (like on)

@Scheduled to declare this is a task, including cron, fixDelay, fixRate other types (the method, you must first open the Scheduled Tasks Support)

10. @ Enable * explanatory notes

These annotations primarily to enable support for the xxx.
@EnableAspectJAutoProxy turn on automatic proxy support for AspectJ

@EnableAsync open support asynchronous method

@EnableScheduling open Scheduled Tasks support

@EnableWebMvc open configuration support of Web MVC

@EnableConfigurationProperties turn supports the configuration of @ConfigurationProperties Bean notes

@EnableJpaRepositories open support for the SpringData JPA Repository

@EnableTransactionManagement open transaction annotation support

@EnableTransactionManagement open transaction annotation support

@EnableCaching open an annotation-caching support

11. The test-related notes

@RunWith runner, Spring is usually used to support for the JUnit

@RunWith (SpringJUnit4ClassRunner.class)
. 1
@ContextConfiguration used to load configuration ApplicationContext, wherein the attribute is used to load the configuration classes Class

@ContextConfiguration (classes TestConfig.class = {})
. 1
SpringMVC portion
@EnableWebMvc open configuration class configuration supports the Web MVC, as some ViewResolver MessageConverter or the like, without sentence, WebMvcConfigurerAdapter override method (for the configuration SpringMVC ).

@Controller class is declared in the Controller SpringMVC

@RequestMapping Web requests for mapping, comprising (a class or method) and the access path parameters

@ResponseBody support the return value placed in the response, rather than a page, usually the user returns json data (return value near or method)

@RequestBody allowed request parameters in the request body, rather than directly connected after the address. (On the front of the parameter)

@PathVariable parameters for the receive path, such @RequestMapping ( "/ hello / {name}") path before stated, the annotation on the parameters, to obtain the value, as is generally the interface method Restful.

@RestController The annotation is a combination of notes, equivalent to a combination of @Controller and @ResponseBody, and notes on the class, which means all of the methods of the Controller will assume added @ResponseBody.

@ControllerAdvice by the annotation, we can place the profile for the global controller in the same position, the annotation @Controller method, can be utilized @ ExceptionHandler, @ InitBinder, @ ModelAttribute annotation to the method,
which all annotated! the method in the controller RequestMapping effective.

@ExceptionHandler used for global exception handling in the controller

@InitBinder to set WebDataBinder, WebDataBinder reception request parameters used to automatically bind the Model.

Originally role @ModelAttribute is to bind key-value pairs in the Model, is to let the global @RequestMapping have access to the key set here in @ControllerAdvice in.

@CrossOrigin to solve cross-domain problems can only be declared on the interface, instead of worrying too much or intercept way.

Guess you like

Origin www.cnblogs.com/ITSeed/p/11109507.html