Home > Back-end >  The Spring and is commonly used for SpringMVC annotation
The Spring and is commonly used for SpringMVC annotation

Time:09-16

Annotation itself has no function, like XML, the annotation and XML is a kind of metadata, metadata that explain the data, it is called configuration,

In this paper, the main Spring and relevant common annotations for SpringMVC,


Spring part
1. The statement of the bean annotation

@ Component components, with no clear role
@ Service used in the business logic layer (layer Service)
@ the Repository is used in the data access layer (dao)
@ Controller used in display layer, Controller's statement (C)

2. Injection of the bean annotation
The @autowired: provided by the Spring
@ Inject: provided by JSR - 330
@ the Resource: provided by JSR - 250
Can comment on the set method and attribute, recommend annotations on the properties (at a glance, and write less code),


3. Java configuration related annotation
@ the current class Configuration statement for the Configuration class, equivalent to the Spring Configuration in the form of XML (class)
@ Bean annotation on the method, the return value of a statement of the current methods for a Bean, instead of XML in the way (method)
@ the current Configuration statement for the Configuration, the internal combination of @ Component annotation, which indicates that this class is a bean (class)
@ ComponentScan used for scanning was carried out on the Component, is equivalent to in XML (class)
@ WishlyConfiguration for @ the Configuration and the combination of the @ ComponentScan annotations, can replace the two annotations


4. Aspects (AOP) related annotation
Spring supports AspectJ annotation type section programming,
@ Aspect declare a section (class)
Using the @ After, @ Before @ Around define Suggestions (advice), can be directly to the blocking rule (tangency) as a parameter,
@ After executed After the method (method)
Executed Before the @ Before the method (method)
@ Around the method perform before and after (method)
The @pointcut statement tangent point
In the Java configuration class using the @ EnableAspectJAutoProxy annotation open Spring support for AspectJ agent (class)


5. @ Bean attribute support
@ the Scope set the Spring container how new Bean instance (method, an @ beans)
Its setting types include:
Singleton (single example, only a bean instance of a Spring container, the default mode),
Protetype (each call to create a bean),
Request (web project, for each HTTP Request to create a new bean),
Session (web project, for each HTTP Session to create a new bean),
GlobalSession (give each a new global HTTP session Bean instance)
@ StepScope in Spring Batch and involves
@ PostConstruct is provided by JSR - 250 and executed after the constructor, equivalent to the XML configuration file of the bean initMethod
@ PreDestory provided by JSR - 250, Bean destroyed before implementation, is equivalent to the XML configuration file of the Bean destroyMethod



6. @ Value annotation

@ the Value of property into Value (attributes)
Support for injection as follows:

into normal character
 @ Value (" Michael Jackson ") 
String name;

injection operating system attribute
 @ Value (" # {systemProperties [' OS. The name ']} ") 
String osName;

injection expression results
 @ Value (" # {T (Java. Lang. Math), random () * 100} ") 
String randomNumber;

into other bean properties
 @ Value (" # {domeClass. Name} ") 
String name;

injection file resources
 @ Value (" classpath: com/HGS/hello/test. TXT ") 
The String Resource file.

injection site resources
 @ Value (" http://www.cznovel.com ") 
The Resource url;

injection profile
 @ Value (" ${book. The name} ") 
String bookName;

injection allocation method of use:
(1) write configuration files (test. The properties)
 book. The name="three body" 

2. @ PropertySource loading configuration file (class)
 @ PropertySource (" classpath: com/HGS/hello/test/test. The propertie ") 

3. Still need to configure a bean PropertySourcesPlaceholderConfigurer,



7. Environmental switch
@ Profile by setting the Environment ActiveProfiles to set the current context needs to use the configuration of the Environment, (or methods)
@ Conditional Spring4 definition Condition words you can use this annotation in the bean, by implementing Condition interfaces, and rewrite matches method, to determine whether the bean is instantiated, (methods)


8. Asynchronous
@ EnableAsync configuration class, through this annotation open support for asynchronous tasks, narrative AsyncConfigurer interface (class)
@ Async in the actual execution of bean method using the annotations to claim it is an asynchronous task (method or class all methods will be asynchronous, asynchronous tasks need @ EnableAsync open)


9. Timing task related
@ EnableScheduling used on configuration class, open plan task support (class)
@ Scheduled to claim this is a task, including the cron, fixDelay, fixRate type (must first open plan task method, support)



10. @ the Enable * note that
These annotations are mainly used for open support for XXX,
@ EnableAspectJAutoProxy open support for AspectJ automatic proxy
@ EnableAsync open support asynchronous method
@ EnableScheduling open plan task support
The configuration of the @ EnableWebMvc open Web MVC support
@ EnableConfigurationProperties open support for @ ConfigurationProperties annotation configuration Bean
@ EnableJpaRepositories open to SpringData JPA Repository support
@ EnableTransactionManagement open annotation type transaction support
@ EnableTransactionManagement open annotation type transaction support
@ EnableCaching open annotation type of cache support



11. Test related annotation
@ RunWith runner, in the Spring is often used to support for JUnit
 @ RunWith (SpringJUnit4ClassRunner. Class) 

@ ContextConfiguration to load configuration ApplicationContext, including classes attribute is used to load the configuration class
 @ ContextConfiguration (classes={TestConfig. Class}) 






part for SpringMVC
@ EnableWebMvc open Web MVC configuration support in the configuration class, as some ViewResolver or MessageConverter etc., without this sentence, rewrite WebMvcConfigurerAdapter method (used to for SpringMVC configuration),
@ Controller declared for the Controller, the SpringMVC
@ RequestMapping used for mapping Web requests, including access path and the parameters (or methods)
@ ResponseBody support the return value is placed within the response, rather than a page, usually the user returns to the json data (beside the return value or methods)
@ RequestBody allows the request parameters in the request body, rather than the direct connection behind the address, (before the parameters)
@ PathVariable for receiving path parameters, such as @ RequestMapping ("/hello/{name} ") that path, the annotations on the parameters of the top, you can get the value, usually as a Restful interface implementation method,
@ RestController the annotated as a combination of annotations, equivalent to @ the Controller and the combination of the @ ResponseBody, annotation on the class, means that the Controller of all methods by default with @ ResponseBody,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related