使用setter依赖注入就是通过容器在调用无参数构造函数或无参数static工厂方法来实例化bean 后调用 bean 上的 setter 方法来完成的。
创建一个User普通类。
public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
beans.xml依赖注入
<bean name="user" class="com.ch.User"> <property name="name" value="Tom"/> <property name="age" value="55"/> </bean>
创建测试类,最终输出
User{name=‘Tom’, age=55}
创建一个两个类,User类和Address类
User类
public class User { private Address name; public Address getName() { return name; } public void setName(Address name) { this.name = name; } @Override public String toString() { return "User{" + "name=" + name + '}'; } }
Address类
public class Address { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Address{" + "name='" + name + '\'' + '}'; } }
beans.xml依赖注入
方案一
<bean name="address" class="com.ch.Address"> <property name="name" value="游泳"/> </bean> <bean name="user" class="com.ch.User"> <property name="name" ref="address"/> </bean>
方案二
<bean name="user" class="com.yc.ch.User"> <property name="name" > <bean name="address" class="com.yc.ch.Address"> <property name="name" value="游泳"/> </bean> </property> </bean>
方案三
再创建一个beans1.xml,里面依赖注入Address的setter
<bean name="address" class="com.yc.ch.Address"> <property name="name" value="游泳"/> </bean>
在beans.xml中使用import标签引入
<import resource="beans1.xml" /> <bean name="user" class="com.yc.ch.User"> <property name="name" ref="address"/> </bean>
创建测试类,最终输出
User{name=Address{name=‘游泳’}}
import java.util.Arrays; public class User { private String[] bokks; public String[] getBokks() { return bokks; } public void setBokks(String[] bokks) { this.bokks = bokks; } @Override public String toString() { return "User{" + "bokks=" + Arrays.toString(bokks) + '}'; } }
beans.xml依赖注入
<bean name="user" class="com.yc.ch.User"> <property name="bokks"> <array> <value>水浒传</value> <value>红楼梦</value> <value>西游记</value> <value>三国演义</value> </array> </property> </bean>
创建测试类,输出
User{bokks=[水浒传, 红楼梦, 西游记, 三国演义]}
import java.util.List; import java.util.Map; import java.util.Set; public class User { private List<String> games; private Set<String> hobbys; private Map<String,String> card; public List<String> getGames() { return games; } public void setGames(List<String> games) { this.games = games; } public Set<String> getHobbys() { return hobbys; } public void setHobbys(Set<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } @Override public String toString() { return "User{" + "games=" + games + ", hobbys=" + hobbys + ", card=" + card + '}'; } }
beans.xml依赖注入
<bean name="user" class="com.yc.ch.User"> <property name="hobbys"> <set> <value>计算机</value> <value>赛车</value> <value>弹吉他</value> </set> </property> <property name="card"> <map> <entry key="身份证" value="15236542"/> <entry key="姓名" value="Tom"/> <entry key="性别" value="男"/> </map> </property> <property name="games"> <list> <value>赛车游戏</value> <value>唱歌游戏</value> </list> </property> </bean>
创建测试类,输出
User{games=[赛车游戏, 唱歌游戏], hobbys=[计算机, 赛车, 弹吉他], card={身份证=15236542, 姓名=Tom, 性别=男}}
null注入不是不写name的value值
public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
错误:
<bean name="user" class="com.yc.ch.User"> <property name="name" value=""/> //创建测试类后,输出的是 User{name=''} </bean>
正确:
<bean name="user" class="com.yc.ch.User"> <property name="name"> <null/> //创建测试类后,输出的是 User{name='null'} </property> </bean>
Properties类主要是读取配置文件的。
import java.util.Properties; public class User { private Properties info; public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "User{" + "info=" + info + '}'; } }
neans.xml依赖注入
<bean name="user" class="com.yc.ch.User"> <property name="info"> <props> <prop key="id">123456</prop> <prop key="姓名">Tom</prop> <prop key="性别">男</prop> </props> </property> </bean>
创建测试类,输出
User{info={性别=男, 姓名=Tom, id=123456}}