首页 > 代码库 > Spirng setter 注入简单

Spirng setter 注入简单

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=http://www.mamicode.com/“值”

	<!-- setter注入 -->
	<bean id="bean2" class="com.hao947.bean.Bean2">
		<property name="name" value=http://www.mamicode.com/"hao947"/>>

<propertyname = “属性” value=http://www.mamicode.com/”值”>


测试

	@Test
	public void show_setter() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Bean2 bean2 = (Bean2) ac.getBean("bean2");
		bean2.show();
	}