spring之set注入
实体类
package pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
//@SuppressWarnings("all")
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public void show(){
System.out.println("name="+ name
+ ",address="+ address.getAddress()
+ ",books="
);
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println("\n爱好:"+hobbys);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("wife:"+wife);
System.out.println("info:"+info);
}
}
package pojo; public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
配置文件
<?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="address" class="pojo.Address"> <property name="address" value="河北省保定市唐县"/> </bean> <bean id="student" class="pojo.Student"> <property name="name" value="窦一萌"/> <property name="address" ref="address"/> <property name="books"> <array> <value>三国演义</value> <value>水浒传</value> <value>红楼梦</value> </array> </property> <property name="card"> <map> <entry key="身份证号" value="11111111111"/> <entry key="银行卡号" value="11111111111"/> </map> </property> <property name="games"> <set> <value>王者荣耀</value> <value>实况足球</value> </set> </property> <property name="hobbys"> <list> <value>踢足球</value> <value>羽毛球</value> <value>跑步</value> </list> </property> <property name="wife"> <null/> </property> <property name="info"> <props> <prop key="学号">20193111</prop> <prop key="工号">123456</prop> </props> </property> </bean> </beans>
测试类
@Test public void DiTest(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml"); Student student = (Student) context.getBean("student"); student.show(); }
运行结果:


浙公网安备 33010602011771号