1. 提供对应要注入的属性
//setter注入
public class Bean2 {
private String name;
private Integer age;
// 提供要注入的属性对应的setter方法
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void show() {
System.out.println("bean2:" + name + "," + age);
}
}
2. 为每个要注入的属性提供对应的标准封装setter方法
3. 在配置中为Bean指定要注入的属性,使用property元素name=“属性”value=“值”
<!-- setter注入 -->
<bean id="bean2" class="com.hao947.bean.Bean2">
<property name="name" value="hao947"/>
<property name="age" value="947"></property>
</bean>
<propertyname = “属性” value=”值”>
测试
@Test
public void show_setter() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean2 bean2 = (Bean2) ac.getBean("bean2");
bean2.show();
}