A detailed description of common annotations in Spring, spring annotations
Description of the configuration file when annotations are used in spring:
<?xml version="1.0" encoding="UTF-8"?> <span style="font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <aop:aspectj-autoproxy/> <context:annotation-config/> <context:component-scan base-package="com.test" /> </beans>
<Context: component-scan/> Configure scan for the specified package to implement dependency injection.
The following describes the usage of some common Annotations:
@Autowired private IReportService reportService ;
Spring2.5 introduces the @ Autowired annotation, which can mark class member variables, methods, and constructors to complete automatic assembly. In addition, @ Autowired can be used to eliminate the get and set methods. @ Autowired: auto-renewal is performed based on the type.
Note that @ Resource can be automatically assembled, but @ Resource is automatically assembled by name by default.
Here we will briefly introduce what is assembly by type and what is assembly by name?
The so-called by type means that when the Spring container contains a bean of the same type as the specified property, the property is automatically assembled. If there are multiple beans of this type, an exception occurs, it is pointed out that automatic assembly by type cannot be used; if no matching bean is found, nothing will happen.
Automatically assemble by name, that is, automatically assemble according to the attribute name. This item checks beans in the Spring container that are exactly the same as the attribute name for automatic assembly.
@Component("reportAction") @Scope("request") public class ReportAction extends AbstractBaseAction
@ Component annotation (the following explanation is from the official documentation)
@ Component, @ Service, and @ Controller. @ Component is a common form of all Spring management components;
@ Repository, @ Service, and @ Controller are the details of @ Component to indicate more specific use cases (for example, they correspond to the persistence layer, Service layer, and performance layer respectively ). That is to say, you can use @ Component to annotate your Component class,
However, if you use @ Repository, @ Service, or @ Controller to annotate them, your class may be better processed by tools or associated with the aspect.
For example, these typical annotations can be an ideal starting point. Of course, in Versions later than Spring Framework, @ Repository, @ Service, and @ Controller may also carry more meanings. In this way, if you are considering whether to use @ Component or @ Service in the Service layer,
Then @ Service is obviously a better choice. Similarly, as mentioned above, @ Repository can be used as a flag for exception conversion in the persistence layer.
@ Scope () specifies the bean Scope.
@ Scope ("prototype") Annotation
Spring containers will re-generate a new object to the requester each time they output prototype objects. Although the instantiation and attribute settings of this type of objects are all completed by the Spring container, however, as long as the preparation is complete and the object instance is returned to the requester, the container will no longer hold the reference of the current object. The requester needs to manage the subsequent lifecycle of the current object by himself, including the destruction of the object. The most typical case is to change the scope of action to prototype when integrating the Spring container with Struts2.
@ Scope ("singleton") Annotation
This value indicates that only one instance exists in the container, and all referenced beans are single instances. This is a typical Singleton model.
@ Scope ("request") Annotation
<Bean id = "requestPrecessor" class = "... RequestPrecessor" scope = "request"/>
The Spring container, that is, XmlWebApplicationContext, is the new RequestPrecessor. When the request ends, the lifecycle of the object ends. When 100 Http requests are sent at the same time, the container will create different RequestPrecessor instances for each request. They do not interfere with each other. In short, the request can be seen as a prototype exception, the application scenario of the request is more specific. There is no difference between the other scenarios.
@ Scope ("session") Annotation
For a Web application, the login information of the user is the most in the session. For information stored in the session, you can change the scope type to session, the Spring container will create its own new UserPreference instance for each independent session, which will survive longer than the bean of the request type. It is similar to the session in javaweb.
Finally, let's talk about why @ Respository can only be labeled on the Dao layer, because the annotation provided by Spring not only recognizes the class as Bean, but also
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!