Spring dependency Injection
Use constructor Injection
Use the setter Method for property Injection
Use Field Injection (for annotation)
In fact, there is another third case of injection, that is, field injection, which is generally used for annotation.
Summary:
Manual assembly or automatic assembly can be used to inject dependent objects. manual assembly is recommended in actual applications, because automatic assembly will produce unknown conditions and developers cannot predict the final assembly result.
Therefore, we recommend that you use manual assembly. Manual Assembly refers to the following: if we want to inject values, we need to do it manually and write the configuration manually,
Dependency injection-manual assembly
Manually assemble dependent objects. There are two programming methods in this method.
1. In the xml configuration file, configure it under the bean node, as shown in figure
<Bean id = "orderservice" class = "CN. itcast. service. orderservicebean "> <constructor-Arg Index =" 0 "type =" Java. lang. string "value =" XXX "/> // constructor injection <property name =" name "value =" Zhao/> // property setter method injection </bean>
2. Use @ autowired or @ resource annotation in Java code for assembly. However, we need 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://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
The first method is to inject data in XML. What is the difference? If we inject data in an XML file, if there are many beans, the xml configuration file will be very bloated. So spring provides us with a weight loss solution in both 2.0 and 2.5, that is, using annotations to reduce the bloated configuration file.
If we don't want to make the configuration file so bloated, we can use annotations to inject dependent objects.
Next, let's take a look at how to assemble with annotations:
If you want to use annotations, you must add these two namespaces.
Xmlns: context = "http://www.springframework.org/schema/context"
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd"
Because we need to open the <context: annotation-config/> annotation .. Provides annotation configuration items. This configuration item is under the context namespace, so we need to introduce the xmlns: context = "http://www.springframework.org/schema/context" namespace, there are two schema files under the namespace
<Context: annotation-config/> what exactly does this configuration item do?
This configuration implicitly registers multiple processors for annotation parsing: autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor.
Each processor corresponds to some annotations, such as autowiredannotationbeanpostprocessor, which is specially used to parse the annotation @ autowired. Commonannotationbeanpostprocessor This processor is specially used to parse the annotation @ resource; persistenceannotationbeanpostprocessor This processor is used to process the annotation of persistence ;..... Each processor processes the corresponding annotation
You must note that the annotation works the same way as XML and is used for configuration. It cannot work. The reason why it can work is that there are these processors behind the CC cream in the grapefruit house. In the future, we will reveal the principle of spring Injection Using annotations.
First, let's take a look at how to inject dependency objects using spring annotations,
Because we need to use annotations, we need to use some jar files. Read the previous ppt and write:
If you use annotations in the JSR-250, such as @ resource/@ postconstruct/@ predestroy, you also need the following JAR files
Lib \ J2EE \ common-annotations.jar
Dependency injection-manual assembly
In Java code, @ autowired or @ resource annotation is used for assembly. The difference between the two annotations is: @ autowired is assembled by type by default, and @ resource is assembled by name by default, only beans with matching names can be assembled by type.
@ Autowired private persondao; // used for fields @ autowired public void setorderdao (orderdao) {// This. orderdao = orderdao ;}
@ Autowired annotation is an assembly of dependent objects 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. If you want to use name-based assembly, you can use it together with the @ qualifier annotation. As follows:
@ Autowired @ qualifier ("persondaobean ")
Private persondao;
The @ resource annotation is the same as the @ autowired annotation. It can also be labeled on the setter method of the field or attribute, but 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, when the annotation is marked on the field, that is, the field name is used as the bean name by default to find the dependent object, when the annotation is marked on the property setter method, that is, the property name is used as the bean name by default 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 assemble by type. However, once the name attribute is specified, it can only be assembled by name.
Beans. xml
<? XML version = "1.0" encoding = "UTF-8"?> <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/beanshttp://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/> <! -- This configuration item must be enabled to use annotations. With this configuration item, you can open the annotation-oriented processor and inject it into the spring container --> <bean id = "persondaoxxxx" class = "CN. itcast. dao. impl. persondaobean "> </bean> <bean id =" personservice "class =" CN. itcast. service. impl. personservicebean "> <! -- <Constructor-Arg Index = "0" type = "CN. itcast. dao. persondao "ref =" persondao "/> <constructor-Arg Index =" 1 "value =" Chuanzhi podcast "/> --> </bean> </beans>
Persondao. Java
package cn.itcast.dao; public interface PersonDao { public void add(); }
Persondaobean. Java
Package CN. itcast. dao. impl; import CN. itcast. dao. persondao; public class persondaobean implements persondao {public void add () {system. out. println ("the add () method in the persondaobean is executed ");}}
Personservice. Java
package cn.itcast.service; public interface PersonService { public void save(); }
Personservicebean. Java
Package CN. itcast. service. impl; import javax. annotation. resource; import CN. itcast. dao. persondao; import CN. itcast. service. personservice; public class personservicebean implements personservice {@ resource // the prefix of the resource annotation package is javax, which is an annotation provided by J2EE, this annotation does not belong to spring // we recommend that you use this annotation when injecting objects using annotation Methods later, instead of using @ autowired annotation // @ autowired is provided by spring; @ resource is provided by J2EE. In jdk1.6, both annotations have the same effect. @ resource does not belong to spring, so it is not closely coupled with the Framework, therefore, we recommend that you use this private persondao; private string name; Public void setpersondao (persondao) {This. persondao = persondao;} public personservicebean () {} public personservicebean (persondao, string name) {This. persondao = persondao; this. name = Name;} public void save () {persondao. add ();}}
Springtest. Java
package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.service.PersonService; public class c{ @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); // ctx.close(); } }
Run the unit test code. The console outputs: "The add () method in the persondaobean is executed"
This injection method is the most elegant and recommended method.
Http://dl.vmall.com/c0kbx6xtd6
Http://www.java63.com/spring/resource_annotation_assemble_property.html