Spring基于构造函数的依赖注入(DI)
当容器调用带有一组参数的类构造函数时,基于构造函数的DI就完成了,其中每个参数代表一个对其他类的依赖。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId> <artifactId>testconstructor</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>testconstructor</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project>
SpellChecker.java:
package com.jsoft.testspring.testconstructor; public class SpellChecker { public SpellChecker(){ System.out.println("SpellChecker无参数构造函数初始化"); } public void checkSpelling(){ System.out.println("SpellChecker检查方法"); } }
TextEditor.java:
package com.jsoft.testspring.testconstructor; public class TextEditor { private SpellChecker spellChecker; public TextEditor(SpellChecker spellChecker){ System.out.println("TextEditor有参数构造函数初始化"); this.spellChecker = spellChecker; } public void spellCheck() { this.spellChecker.checkSpelling(); } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="spellChecker" class="com.jsoft.testspring.testconstructor.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testconstructor.TextEditor"> <constructor-arg ref="spellChecker"></constructor-arg> </bean> </beans>
这里直接采用<constructor>节点指定TextEditor构造函数的参数。
App.java:
package com.jsoft.testspring.testconstructor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! * */ public class App { public static void main( String[] args ) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor"); textEditor.spellCheck(); } }
运行结果:
构造函数参数解析:
如果存在不止一个参数时,当把参数传递给构造函数时,可能会存在歧义。要解决这个问题,那么构造函数的参数在bean定义中的顺序,就是把这些参数提供给适当的构造函数参数的顺序对应上就可以了。考虑下面的类:
package x.y; public class Foo { public Foo(Bar bar, Baz baz) { // ... } }
下述配置文件是能正常运行的:
<beans> <bean id="foo" class="x.y.Foo"> <constructor-arg ref="bar"/> <constructor-arg ref="baz"/> </bean> <bean id="bar" class="x.y.Bar"/> <bean id="baz" class="x.y.Baz"/> </beans>
只要把配置文件参数的顺序对应上构造函数参数的顺序即可。
让我们再考虑一下我们传递给构造函数不同类型的位置。参考下面的类:
package x.y; public class Foo { public Foo(int year, String name) { // ... } }
如果你使用type属性显式的指定了构造函数参数的类型,容器也可以使用与简单类型匹配的类型。例如:
<beans> <bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="2001"/> <constructor-arg type="java.lang.String" value="Zara"/> </bean> </beans>
最后,最好的传递构造函数参数的方式,是使用index属性来显式的指定构造函数参数的索引。下面是基于索引为0的例子,如下所示:
<beans> <bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="2001"/> <constructor-arg index="1" value="Zara"/> </bean> </beans>
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test8/testconstructor