Spirng setter 注入简单

来源:互联网 发布:淘宝首页轮播图尺寸 编辑:程序博客网 时间:2024/06/07 07:22

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=”值”>


测试

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


0 0
原创粉丝点击