Using annotation Assembly attributes in spring2.5
It can be used in Java code@ ResourceOr@ AutowiredAnnotations for assembly, but the following information must be configured in XML:
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemalocation = "http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd"
Then explicitly configure<Context: annotation-config/>
This configuration implicitly registers multiple processors for annotation parsing, as listed below
Autowiredannotationbeanpostprocessor commonannotationbeanpostprocessor
Persistenceannotationbeanpostprocessor requiredannotationbeanpostprocessor
In fact, annotation itself can't do anything. Like XML, it only serves as a configuration, mainly because of the powerful processor behind it.
In addition,@ Resource annotation is recommended for comparison.,Instead of using the @ autowired Annotation
Because the @ autowired annotation is provided by spring, and the @ resource annotation is provided by J2EE
The @ resource annotation has been included in JDK 6, so it is not closely coupled with spring.
If spring is usedJSR-250Annotations in,For example, @ resource // @ postconstruct // @ predestroy
In the spring installation directorySpring_home // lib // J2EE // common-annotations.jar Package Support
Here the @ resource annotation is in the spring_home // lib // J2EE // common-annotations.jar
@ Resource Annotation
The @ resource annotation is the same as the @ autowired annotation. It can also be marked on the setter method of the field or attribute.
@ ResourceAssembly by name by default, The name can beName attribute. If the bean matching the name cannot be found, it is assembled by type.
If the annotation is marked on the field and the name attribute is not specifiedBy default, the field name is used as the bean name to find the dependent object.
If the annotation is marked on Setter and the name attribute is not specifiedBy default, the property name is used as the bean name to find the dependent object.
If the name attribute is not specified and the dependent object cannot be found by default name, it will match by type
However, if the name attribute is specified, it can only be assembled by name.
@ Autowired Annotation
@ AutowiredBy default, objects are assembled by type.By default, it requires that the dependent object must exist.
If the null value is allowed, you can set its required attribute to false, as shown in figure@ Autowired (required = false)
To assemble by name, use the @ qualifier annotation together, as shown in figure@ Autowired (required = false) @ qualifier ("persondaobean ")
Automatically scans spring2.5 Components
In a slightly larger project, there are usually hundreds of components.XML BeanDefine to configure Components
Obviously, it will increase the size of the configuration file and make it inconvenient to search and maintain it.
WhileSpring2.5We introducedAutomatic component Scanning
It can be stored inClasspathMarked@ Service, @ repository, @ controller, @ component AnnotationClass
And add these classes to the spring container for management. It serves the same purpose as using bean node configuration components in XML.
To use the automatic scanning mechanism, you must configure<Context: component-scan base-package = "com. jadyer"/>Enable Automatic Scanning
WhereBase-PackageSpecifies the package to be scanned. It scans the class in the specified package and the class in the sub-package.
@ ServiceUsed to mark service layer components
@ RepositoryUsed to mark the data access component (DAO component)
@ ControllerUsed to mark control layer components, such as actions in struts
@ ComponentGeneric Components. You can use this annotation to label components when they are not well classified.
1,You can use@ Service ("persondao ")Modify bean nameBy default, the lower-case class name of the first letter is used as the <bean> name.
2,ToChange <bean> ScopeYou can use@ Scope ("prototype ")Annotation to modify <bean> Scope
3,ToLet the initialization method be executed after <bean> instantiation, You can use@ PostconstructMark on Method
4,Similarly@ PredestroyAnnotation can be used in methods.Specifies the method to be executed when <bean> is destroyed
Here @ postconstruct is the annotation used in ejb3 to initialize the bean. It is not the annotation in spring.
And <context: component-scan base-package = ""/>Many processors are registered behind the scenes for parsing annotations.
This includes the processors used for the annotations in the <context: annotation-config/> configuration item.
After <context: component-scan base-package = ""> is configured,You do not need to configure <context: annotation-config>