当容器调用带有一组参数的类构造函数时,基于构造函数的 di 就完成了,其中每个参数代表一个对其他类的依赖。
接下来,我们将通过示例来理解 spring 基于构造函数的依赖注入。
下面的例子显示了一个类 texteditor,只能用构造函数注入来实现依赖注入。
让我们用 eclipse ide 适当地工作,并按照以下步骤创建一个 spring 应用程序。
步骤 | 描述 |
---|---|
1 | 创建一个名为 springexample 的项目,并在创建的项目中的 src 文件夹下创建包 com.tutorialspoint 。 |
2 | 使用 add external jars 选项添加必需的 spring 库,解释见 spring hello world example chapter. |
3 | 在 com.tutorialspoint 包下创建 java类 texteditor,spellchecker 和 mainapp。 |
4 | 在 src 文件夹下创建 beans 的配置文件 beans.xml 。 |
5 | 最后一步是创建所有 java 文件和 bean 配置文件的内容并按照如下所示的方法运行应用程序。 |
这是 texteditor.java 文件的内容:
package com.tutorialspoint;
public class texteditor {
private spellchecker spellchecker;
public texteditor(spellchecker spellchecker) {
system.out.println("inside texteditor constructor." );
this.spellchecker = spellchecker;
}
public void spellcheck() {
spellchecker.checkspelling();
}
}
下面是另一个依赖类文件 spellchecker.java 的内容:
package com.tutorialspoint;
public class spellchecker {
public spellchecker(){
system.out.println("inside spellchecker constructor." );
}
public void checkspelling() {
system.out.println("inside checkspelling." );
}
}
以下是 mainapp.java 文件的内容:
package com.tutorialspoint;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class mainapp {
public static void main(string[] args) {
applicationcontext context =
new classpathxmlapplicationcontext("beans.xml");
texteditor te = (texteditor) context.getbean("texteditor");
te.spellcheck();
}
}
下面是配置文件 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-3.0.xsd">
<!-- definition for texteditor bean -->
<bean id="texteditor" class="com.tutorialspoint.texteditor">
<constructor-arg ref="spellchecker"/>
</bean>
<!-- definition for spellchecker bean -->
<bean id="spellchecker" class="com.tutorialspoint.spellchecker">
</bean>
</beans>
当你完成了创建源和 bean 配置文件后,让我们开始运行应用程序。如果你的应用程序运行顺利的话,那么将会输出下述所示消息:
inside spellchecker constructor.
inside texteditor constructor.
inside checkspelling.
如果存在不止一个参数时,当把参数传递给构造函数时,可能会存在歧义。要解决这个问题,那么构造函数的参数在 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>
最后,如果你想要向一个对象传递一个引用,你需要使用 标签的 ref 属性,如果你想要直接传递值,那么你应该使用如上所示的 value 属性。