This article describes the methods for injecting attributes using spring annotations. Before using annotations, injected properties are implemented through classes and configuration files. Injection properties can now be implemented by introducing @autowired annotations, or @resource, @Qualifier, @PostConstruct, @PreDestroy, and so on. 1.1. The implementation of the class using annotations before we injected the attribute: 1public class Usermanagerimpl implements Usermanager {2 private Userdao Userdao; 3 public void Setuserdao (Userdao userdao) {4 This.userdao = Userdao; 5} 6 ... 7} configuration file: 1<BeanID= "Usermanagerimpl"class= "Com.kedacom.spring.annotation.service.UserManagerImpl">2< Propertyname= "Userdao"ref= "Userdao" />3</Bean>4<BeanID= "Userdao"class= "Com.kedacom.spring.annotation.persistence.UserDaoImpl">5< Propertyname= "Sessionfactory"ref= "Mysessionfactory" />6</Bean>1.2. Introduction of @autowired annotations (deprecated, recommended to use @resource) class implementations (to label member variables) 1public class Usermanagerimpl implements Usermanager {2 @A Utowired3 private Userdao Userdao; 4 ... 5} or (Annotate method) 1public class Usermanagerimpl implements Usermanager {2 private Userdao Userdao; 3 @Autowired4 Publ IC void Setuserdao (Userdao userdao) {5 This.userdao = Userdao; 6} 7 ... 8} configuration file 1<BeanID= "Usermanagerimpl"class= "Com.kedacom.spring.annotation.service.UserManagerImpl" />2<BeanID= "Userdao"class= "Com.kedacom.spring.annotation.persistence.UserDaoImpl">3< Propertyname= "Sessionfactory"ref= "Mysessionfactory" />4</Bean>@Autowired can label member variables, methods, and constructors to accomplish the task of automating Assembly. In the two different implementations, the @Autowired label positions are different, they will automatically assemble the Userdao attribute when spring initializes the Usermanagerimpl bean, the difference being: the first implementation, Spring directly assigns the only bean of the Userdao type to the member variable of Userdao, and in the second implementation, spring calls the Setuserdao method to assemble the only bean of the Userdao type into the Userdao property. 1.3. Let @autowired work to enable @autowired to work, and add the following code to the configuration file 1<Beanclass= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />1.4. @Qualifier the @autowired is automatically assembled according to the type. In the example above, if there is more than one Userdao type of bean in the spring context, a Beancreationexception exception is thrown, and if a bean of type Userdao is not present in the spring context, it will be thrown Beancreationexception exception. We can use @qualifier with @autowired to solve these problems. A. Multiple Userdao instances may exist 1@autowired2public void Setuserdao (@Qualifier ("Userdao") Userdao Userdao) {3 This.userdao = Userdao; 4} In this way, spring will find the bean with ID userdao to assemble. B. The Userdao instance may not exist 1@autowired (required = false) 2public void Setuserdao (Userdao Userdao) {3 This.userdao = Userdao; 4}1 .5. @Resource (JSR-250 standard note, which is recommended instead of spring's proprietary @autowired annotations) spring supports its own defined @autowired annotations, as well as several annotations defined by the JSR-250 specification. They are @resource, @PostConstruct and @predestroy, respectively. The function of the @Resource is equivalent to @autowired, but @autowired is automatically injected by Bytype, and @resource is automatically injected by ByName by default. It is important to @Resource that there are two properties, namely name and type,spring, which resolves the name property of the @resource annotation to the bean, while the type attribute resolves to the bean. So if you use the Name property, you use the ByName Auto-injection policy, and the Type property uses the Bytype auto-injection policy. If neither name nor the type attribute is specified, the ByName auto-injection policy is used through the reflection mechanism. @Resource Assembly Order 1. If both name and type are specified, the only matching bean found in the spring context is assembled and an exception of 2 is thrown if it is not found. If name is specified, look for the name (ID) match from the context BEAN is assembled, and the exception 3 is thrown when it is not found. If type is specified, an exception of 4 is thrown when a unique bean of type matching is found in the context to be assembled, not found or more than one is found. If neither name is specified and type is not specified, the assembly is automatically byname (see 2), and if there is no match, the fallback is matched to an original type (Userdao) and automatically assembled if matched; 1.6. @PostConstruct (JSR-250) adds annotation @postconstruct to the method, this method is executed by the spring container after the bean is initialized (note: Bean initialization includes, instantiates beans, And assemble the Bean's properties (Dependency injection)). A typical application scenario is when you need to inject a property defined in the bean into a parent class, and you cannot replicate the setter method of the parent class's property or property, such as: 01public class Userdaoimpl extends Hibernatedaosupport implements Userdao {private sessionfactory mysessionfacotry; @Resource04 public void S Etmysessionfacotry (Sessionfactory sessionfacotry) {this.mysessionfacotry = Sessionfacotry; @PostC onstruct08 public void Injectsessionfactory () {super.setsessionfactory (mysessionfacotry); 10} 11 ... 12} Here through @postconstruct, a sessionfactory private property defined for Userdaoimpl's parent class, Injected with our own defined sessionfactory (the parent class's Setsessionfactory method is final, non-reproducible), then we can access the property by calling Super.getsessionfactory (). 1.7. @PreDestroy (JSR-250) adds annotation @predestroy to the method, which is executed by the spring container after the bean is initialized. Since we do not currently have a scene to use it, hereNo, not a demo. Its usage is the same as @postconstruct. 1.8. Use<Context:annotation-config/>Simplified Configuration Spring2.1 Adds a new context schema namespace that provides a convenient configuration for annotation drivers, attribute file ingestion, load-time weaving, and more. We know that the annotation itself will not do anything, it only provides meta-data information. For the metadata information to really work, you must let the processor responsible for processing the metadata work. Autowiredannotationbeanpostprocessor and Commonannotationbeanpostprocessor are processors that process these annotation metadata. But defining these beans directly in the spring configuration file looks awkward. Spring provides us with a convenient way to register these beanpostprocessor, which is<Context:annotation-config/>: 1<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"2 xsi:schemalocation= "Http://www.springframework.org/schema/beans 3 http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd 4 Http://www.springframework.org/schema/context 5 http://www.springframework.org/schema/ Context/spring-context-2.5.xsd ">6<Context:annotation-config/>7</Beans><Context:annotationconfig/>the spring container is implicitly registered with the Autowiredannotationbeanpostprocessor, Commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor and Requiredannotationbeanpostprocessor the 4 beanpostprocessor. Original: http://www.blogjava.net/hellxoul/archive/2011/11/21/364419.html
Spring AOP annotations are explained through @autowired, @Resource, @Qualifier, @PostConstruct, @PreDestroy the configuration file of the injected properties