Comments on spring
We might as well classify the annotations commonly used in spring according to their functions
1. Add ordinary classes to the container to form bean annotations
Annotations for defining beans mainly used in daily development include (XML configuration of beans will not be discussed temporarily):
@Component、@Repository、@Service、@Controller、@Bean
Among them, @ component, @ repository, @ service and @ controller are essentially the same type of annotations, with the same usage and functions. The difference is to identify the type of component. When a component represents the data access layer (DAO), you can annotate it with @ repository, as shown in the following example:
When the component is used in the business layer, use the @ service annotation, as shown in the following example:
Note that the @ service annotation here adds an additional attribute of value = "secuserservice". Why do you do this? The value attribute can specify the name of the generated bean, which becomes the unique identifier of the bean in the container. Similarly, you can also specify the value value in @ component, @ repository and @ controller. Of course, add it if necessary
When the component belongs to the control layer, the @ controller annotation is used; When components cannot be well classified, we can use @ Component annotation. Because the use methods are the same, the code will not be pasted here
Among these annotations, the @ bean annotation needs extra attention. Look at the code:
Bean annotation is mainly used for methods, which is somewhat similar to factory methods. When @ bean annotation is used, we can continuously use a variety of annotations used to define beans, such as defining the name of factory methods with @ qualifier annotation and defining the scope of the bean with @ scope annotation, such as singleton or prototype.
There is another note here: @ configuration In fact, the @ bean annotation mentioned above is mostly used with the @ configuration annotation. If a class is marked as @ configuration annotation, it means that this class will be used as a factory for creating various beans (similar to a new container). The simplest examples of cooperation are as follows:
The above code is actually equivalent to the configuration in XML:
We should also note that there is a certain difference between using @ bean in spring's @ Component annotation and using @ configuration in spring. Cglib proxy interception methods and properties are not enforced in classes that use @ component. In the @ configuration class, cglib proxy will be used to call the method marked by @ bean and return the reference of the object. Using @ bean in the @ configuration annotation can also prevent subtle and difficult errors when the same @ bean method is accidentally called multiple times
2. Annotations commonly used when fetching beans (assembly beans) from containers
The most commonly used annotations for assembly in development are: @ Autowired and @ resource
@Autowired annotation:
@Autowired annotation can be used to annotate the properties, constructors and methods of a class. By default, The object on which it depends must exist (beans are available). If you need to change this default method, you can set its required property to false. Another important point is that the @ Autowired annotation is assembled by type by default. If the container contains multiple beans of the same type, an exception will be reported when the container is started. The solution is to limit it in combination with the @ qualified annotation, which means Name of the bean to be injected
@Resource annotation for @ resource annotation, it does not belong to spring annotation, but comes from jsr-250. By default, it is injected according to the name of the bean. When no match is found, it will be assembled according to the type. When assembling by name, you can specify its name attribute. If it is not specified, the default name of the field on which the annotation is marked is the name of that field. Of course, the @ resource annotation also supports assembly according to the specified type. Just assign a specific type value to its type attribute (note that when the name attribute is specified, it can only be assembled according to the name)
According to my personal understanding, in fact, @ resource is much easier to use than @ Autowired. The @ resource annotation is more flexible to use. You can specify the name or type. However, it is easy to throw exceptions when assembling with @ Autowired annotation, especially when there are multiple bean types assembled. The solution is to add @ qualified for qualification
VC module Annotation "> 3. Spring MVC module annotation. Common annotations used in web module include:
@Controller、@RequestMapping、@RequestParam、@PathVariable
@After the controller annotates a class with @ controller, it indicates that the class will be used as a control layer component interacting with the front end
@The requestmapping annotation is used to map the URL to the entire processing class or a specific method for processing requests
As in the above example, when it is marked on a class, it indicates that this class will receive a request with a URL of "/ company"
@Requestparam is used to bind the request parameter to the specified method
For example, in the above example, the @ requestparam request parameter specifies the specific field name of the incoming parameter (specified by value) and whether it must be passed (required = true by default). Data and phoneNum are formal parameters, that is, the use name of the request parameter, which can be changed
@Pathvariable this annotation is used to modify method parameters, which will change the modified method parameters into available URI variables (which can be used for dynamic binding). See the following figure:
When we request "/ component / Account", the compoyname will be dynamically bound as "account"
4. Transaction module annotation @ transactional
When dealing with transaction operations of Dao layer or service layer, such as rollback operation in case of deletion failure, the @ transactional annotation can be used, as shown in the following example:
The above example shows that transaction processing is enabled when the deletebyname method is executed, and the meanings of transaction attributes are as follows:
Propagation of transactions. Spring specifies seven types of transaction propagation behaviors in the transactiondefinition interface, which specify how to propagate transaction methods and transaction methods when nested calls occur:
Note: the first one is the most commonly used and the default one
The read-write attribute of the readonly transaction is true or false. True is read-only and the default is false. The rollback for rollback policy rolls back when a specified exception is encountered. For example, in the above example, when an exception is encountered, rollback timeout (supplementary) is set, and the unit is seconds. Isolation sets the transaction isolation level and enumeration type. There are five types in total
5. Annotation of spring AOP module annotation of spring AOP module mainly includes @ aspect, @ pointcut, @ before, @ around, @ after, @ afterreturning, @ afterthrowing
@Aspect indicates that this class is an aspect class and enables AspectJ annotation. Note: it should be used together with @ component, otherwise it will not be scanned and added to the container
@Pointcut defines pointcuts. For the writing method of pointcut expressions, please move to the official document: Spring AOP document
@Around defines the surround notification and embeds relevant business logic before and after the execution of the target method
@Before defines the pre notification, which is executed before the target method is executed
@After defines the post notification, which is executed after the target method is executed. Whether the target method exits after normal execution or after an exception is thrown, it will be executed
@The afterreturning target method executes normally. After exiting, execute @ afterthrowing target method executes. After throwing an exception, execute
summary
The above is all about the common annotations of spring in this paper. I hope it will be helpful to you. Interested friends can continue to see:
Java spring annotation configuration bean instance code parsing
On Java annotation and dynamic proxy