Set注入
要求被注入的属性,在对应的类中必须有set方法
案例放分析:
实体类
public class address {
private String address;
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
public class student {
private String name;
private address address;
private String[]book;
private List<String> hobby;
private Map<String,String>idCard;
private Set<String>games;
private Properties properties;
public void setName(String name) {
this.name = name;
}
public void setAddress(cn.com.Ycy.spring.domain.address address) {
this.address = address;
}
public void setBook(String[] book) {
this.book = book;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public void setIdCard(Map<String, String> idCard) {
this.idCard = idCard;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", address=" + address.getAddress() +
", book=" + Arrays.toString(book) +
", hobby=" + hobby +
", idCard=" + idCard +
", games=" + games +
", properties=" + properties +
'}';
}
}
注入分类
1、常量注入
<bean id="student" class="cn.com.Ycy.spring.domain.student">
<property name="name" value="张三"/>
</bean>
2、引用类型注入
<bean id="address" class="cn.com.Ycy.spring.domain.address">
<property name="address" value="北京"/>
</bean>
<bean id="student" class="cn.com.Ycy.spring.domain.student">
<property name="address" ref="address"/>
</bean>
3、数组注入
<property name="book">
<array>
<value>java开发手册</value>
<value>python入门到精通</value>
<value>围城</value>
<value>斗罗大陆</value>
</array>
</property>
4、List 注入
<property name="hobby">
<list>
<value>看书</value>
<value>听歌</value>
<value>写代码</value>
<value>看动漫</value>
</list>
</property>
5、set 注入
<property name="games">
<set>
<value>王者荣耀</value>
<value>LOL</value>
</set>
</property>
6、Map注入
<property name="idCard">
<map>
<entry key="身份证" value="123455666"/>
<entry key="学号" value="2020704"/>
</map>
</property>
和Java的Map一样,key对应键值
7、Properties 注入
<property name="properties">
<props>
<prop key="学号">20201147</prop>
<prop key="高数A">68</prop>
<prop key="java">88</prop>
</props>
</property>
这个就等同于
学号=20201147
高数A=68
java=88
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="cn.com.Ycy.spring.domain.student">
<property name="name" value="张三"/>
<property name="address" ref="address"/>
<property name="book">
<array>
<value>java开发手册</value>
<value>python入门到精通</value>
<value>围城</value>
<value>斗罗大陆</value>
</array>
</property>
<property name="hobby">
<list>
<value>看书</value>
<value>听歌</value>
<value>写代码</value>
<value>看动漫</value>
</list>
</property>
<property name="idCard">
<map>
<entry key="身份证" value="123455666"/>
<entry key="学号" value="2020704"/>
</map>
</property>
<property name="games">
<set>
<value>王者荣耀</value>
<value>LOL</value>
</set>
</property>
<property name="properties">
<props>
<prop key="学号">20201147</prop>
<prop key="高数A">68</prop>
<prop key="java">88</prop>
</props>
</property>
</bean>
<bean id="address" class="cn.com.Ycy.spring.domain.address">
<property name="address" value="北京"/>
</bean>
</beans>
测试代码:
@Test
public void test02(){
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
student student = (cn.com.Ycy.spring.domain.student) ac.getBean("student");
System.out.println(student);
}
输出结果:
student{name='张三', address=北京,
book=[java开发手册, python入门到精通, 围城, 斗罗大陆],
hobby=[看书, 听歌, 写代码, 看动漫],
idCard={身份证=123455666, 学号=2020704},
games=[王者荣耀, LOL],
properties={java=88, 高数A=68, 学号=20201147}
}