依赖注入Bean属性,使用xml配置
1、构造方法注入
案例代码演示
public class User {
private String username;
private String password;
private Integer age;
public User() {}
public User(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
========================================================================================
通过构造方法注入参数
========================================================================================
测试函数
public class ServiceTest {
public static void main(String[] args) {
ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context1.getBean("user");
System.out.println(user);
}
}
执行测试函数得到如下结果:User{username='zhangsan', password='123456', age=99}
上面的bean.xml还可以通过索引注入参数,其他不变,修改 bean.xml 如下
通过索引注入参数
执行测试函数得到如下结果:User{username='李四', password='112233', age=44}
2、属性setter方法注入(有两种)
上面的 User 和 测试函数 ServiceTest 不变,只需要修改 bean.xml即可
=========================================================================================
11
666
王麻子
上面 两种配置 bean.xml 都可以,采用第一种配置的比较只管方便。测试结果都如下:
User{username='王麻子', password='666', age=11}
3、p命名空间注入
上面的 User 和 测试函数 ServiceTest 不变,只需要修改 bean.xml即可。
这种方法不常用,有兴趣的朋友可以对比下 p 命名空间的 bean.xml 和构造方法注入、setter 方法注入的 bean.xml 区别就是加了一个===》xmlns:p ="http://www.springframework.org/schema/p",就可以使用 p 命名空间了。运行结果如下:
User{username='秦始皇', password='888', age=111}
4、集合注入
List、Set、Map、Properties
public class Coder {
private List cars; // 车
private Set pats; // 宠物
private Map information; // 信息
private Properties mysqlInfo; // Mysql 连接信息
public Properties getMysqlInfo() {
return mysqlInfo;
}
public void setMysqlInfo(Properties mysqlInfo) {
this.mysqlInfo = mysqlInfo;
}
public Map getInformation() {
return information;
}
public void setInformation(Map information) {
this.information = information;
}
public Set getPats() {
return pats;
}
public void setPats(Set pats) {
this.pats = pats;
}
public List getCars() {
return cars;
}
public void setCars(List cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Coder{" +
"cars=" + cars +
", pats=" + pats +
", information=" + information +
", mysqlInfo=" + mysqlInfo +
'}';
}
}
=========================================================================================
ofo
宝马
奔驰
猪
马
牛
mysql:jdbc://localhost:3306/sample
root
root
=========================================================================================测试函数
public class ServiceTest {
public static void main(String[] args) {
ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
Coder coder = (Coder) context1.getBean("coder");
System.out.println("车:"+coder.getCars());
System.out.println("宠物:"+coder.getPats());
System.out.println("个人信息:"+coder.getInformation());
System.out.println("数据库信息信息:"+coder.getMysqlInfo());
}
}
运行结果如下:车:[ofo, 宝马, 奔驰]
宠物:[猪, 马, 牛]
个人信息:{name=王八蛋, age=99, password=8888}
数据库信息信息:{password=root, url=mysql:jdbc://localhost:3306/sample, username=root}