Use Field Injection (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 @ autowired or @ resource to configure the following information in the XML 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 for annotation parsing: autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor
Note: @ resource annotation in the spring installation directory Lib \ J2EE \ common-annotations.jar
@ Autowired and @ resource annotation assembly, the difference is:
@ Autowired is assembled by type by default, and @ resource is assembled by name by default. Only beans with the same name cannot be found will be assembled by type.
@ Autowired
Private persondao is used on fields
@ Autowired
Public void setorderdao (orderdao) {on the setter method used for the attribute
This. orderdao = orderdao;
}
@ Autowired Zhu explains how to assemble dependency objects by type. By default, dependency objects must exist. If null values are allowed, set the required attribute to false, if we use name-based assembly, we can use it together with the @ qualifier annotation. As follows:
@ Autowired @ qualifier ("persondaobean ")
Private persondao;
@ Resource annotation (recommended, is a class in the J2EE package) and @ autowired (is a class in spring) can also be marked on the setter method of the field or attribute, however, it is assembled by name by default. The name can be specified through the name attribute of @ resource. If the name attribute is not specified, the annotation is blocked on the field. By default, the field name is used as the bean name to find the dependent object, block the annotation in the setter method of the attribute. By default, the attribute name is used as the bean name to find the dependent object.
@ Resource (name = "persondaobean ")
Private persondao // used for fields
Note: If the name attribute is not specified and the dependent object cannot be found by default name, the @ resource annotation will be rolled back to the Assembly by type, but once the name attribute is specified, you can only assemble by name.
Http://blog.163.com/ky_199/blog/static/14317602009225520045/