ThanksItcastFree video released.
UseFieldInjection (for annotation)
Manual assembly or automatic assembly can be used to inject dependent objects. manual assembly is recommended in actual applications.
You can also use@ AutowiredOr@ ResourceWe needXMLConfigure the following information in the configuration file:
<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
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<Context: annotation-config/>
</Beans>
This configuration implicitly registers multiple processors that parse comments:Autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor
Note:@ ResourceAnnotation inSpringInstallation DirectoryLib \ J2EE \ common-annotations.jar
@ AutowiredAnd@ ResourceAnnotation Method for assembly, the difference is:
@ AutowiredDefault assembly by type,@ ResourceBy default, names are assembled. If no matching name is foundBeanAssembly by type.
@ Autowired
Private persondaoUsed on fields
@ Autowired
Public void setorderdao (orderdao ){Used for AttributeSetterMethod
This. orderdao = orderdao;
}
@ AutowiredZhu explains to assemble dependent objects by type. By default, it requires that dependent objects exist.NullValue, you can set itsRequiredThe property isFalse,If we want to assemble by name, we can combine@ QualifierAnnotations are used together. As follows:
@ Autowired @ qualifier ("persondaobean ")
Private persondao;
@ Resource Annotation and @ Autowired You can also mark Setter Method, but it is assembled by name by default. The name can be @ Resource Of Name Attribute specified. If not specified Name Attribute to block the Annotation on the field, that is, the default field name is used Bean Find the dependent object by name to block Setter Method, that is, the default attribute name is used Bean Search for dependent objects by name.
@ Resource (name = "persondaobean ")
Private persondao //Used on fields
Note:NameAttribute, and the dependent object cannot be found according to the default name,@ ResourceThe annotation will be rolled back to the by-type assembly, but onceNameAttribute, you can only assemble by name.