本文共 4988 字,大约阅读时间需要 16 分钟。
1. 构造器注入
基于构造器的注入通过调用带参数的构造器来实现,每个参数代表着一个协作者。
1.1 最简单形式
我们以下面的Student实体类为例进行说明:
package com.sjf.bean;
/**
* 学生实体类
* @author sjf0115
*
*/
public class Student {
private String name;
private String company;
private int age;
private boolean sex;
public Student(String name, String company, int age, boolean sex) {
this.name = name;
this.company = company;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("个性详细信息如下:" + "\n");
stringBuilder.append("name:" + name + "\n");
stringBuilder.append("company:" + company + "\n");
stringBuilder.append("age:" + age + "\n");
stringBuilder.append("sex:" + (sex ? "boy" : "girl"));
return stringBuilder.toString();
}
}
<?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 = "yoona" class = "com.sjf.bean.Student">
<constructor-arg value = "yoona"/>
<constructor-arg value = "Facebook"/>
<constructor-arg value = "24"/>
<constructor-arg value = "true"/>
</bean>
</beans>
name:yoona
company:Facebook
age:24
sex:true
public Student(String name, String company) {
this.name = name;
this.company = company;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
<bean id = "yoona" class = "com.sjf.bean.Student">
<constructor-arg value = "yoona"/>
<constructor-arg value = "24"/>
</bean>
name:yoona
company:24
<bean id = "yoona" class = "com.sjf.bean.Student">
<constructor-arg type = "java.lang.String" value = "yoona"/>
<constructor-arg type = "int" value = "24"/>
</bean>
name:yoona
age:24
1.3 根据index属性传值
通过使用index属性可以显式的指定构造器参数出现顺序。指定构造器参数索引是使用构造器IoC首选的方式。
那么现在考虑如下情况。我们在Student类中有如下的构造器:
public Student( int age, String company) {
this.company = company;
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
<bean id = "yoona" class = "com.sjf.bean.Student">
<constructor-arg type = "java.lang.String" value = "yoona"/>
<constructor-arg type = "int" value = "24"/>
</bean>
company:yoona
age:24
<bean id = "yoona" class = "com.sjf.bean.Student">
<constructor-arg index = "0" value = "yoona"/>
<constructor-arg index = "1" value = "24"/>
</bean>
name:yoona
age:24
其实还有一个得推荐的方式:根据name属性进行匹配。针对上面的构造器我们使用name属性进行配置如下:
<bean id = "yoona" class = "com.sjf.bean.Student">
<constructor-arg name = "name" value = "yoona"/>
<constructor-arg name = "age" value = "24"/>
</bean>
name:yoona
age:24
1.5 通过构造器注入对象引用
我们为Student提供一个学校实体类:
package com.sjf.bean;
/**
* 学校实体类
* @author sjf0115
*
*/
public class School {
private String name;
private String location;
public School(String name, String location) {
this.name = name;
this.location = location;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("name:" + name);
stringBuilder.append(" location:" + location);
return stringBuilder.toString();
}
}
<bean id = "xidian" class = "com.sjf.bean.School">
<constructor-arg index = "0" value = "西电"/>
<constructor-arg index = "1" value = "西安"/>
</bean>
package com.sjf.bean;
/**
* 学生实体类
* @author sjf0115
*
*/
public class Student {
private String name;
private School school;
private int age;
public Student(String name, School school, int age) {
this.name = name;
this.school = school;
this.age = age;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("个人详细信息如下:" + "\n");
stringBuilder.append("name:" + name + "\n");
stringBuilder.append("age:" + age + "\n");
stringBuilder.append("school:" + school.toString());
return stringBuilder.toString();
}
}
<bean id = "yoona" class = "com.sjf.bean.Student">
<constructor-arg name = "name" value = "yoona"/>
<constructor-arg name = "age" value = "24"/>
<constructor-arg name = "school" ref = "xidian"/>
</bean>