一个包含多个复杂属性的实体类如下
public class Student {
private String name;
private Address address;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + books +
", hobby=" + hobby +
", cards=" + cards +
", wife='" + wife + '\'' +
", info=" + 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 List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
public Map<String, String> getHobby() {
return hobby;
}
public void setHobby(Map<String, String> hobby) {
this.hobby = hobby;
}
public Set<String> getCards() {
return cards;
}
public void setCards(Set<String> cards) {
this.cards = cards;
}
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;
}
private List<String> books;
private Map<String,String> hobby;
private Set<String> cards;
private String wife;
private Properties info;
}
一、直接值注入:
在Beans.xml中直接注入:
<bean id="student" class="com.tt.pojo.Student">
<property name="name" value="晚安世界"/>
</bean>
在测试类里面拿到student对象,取出name属性的值
public static void main(String[] args) {
//获取ApplicationContext对象,拿到Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
二、Set注入
<bean id="address" class="com.tt.pojo.Address">
<property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.tt.pojo.Student">
<!-- 普通值注入-->
<property name="name" value="晚安世界"/>
<!-- Bean注入-->
<property name="address" ref="address"/>
<!-- 数组注入 -->
<property name="books" >
<array>
<value>水浒传</value>
<value>西游记</value>
<value>三国演义</value>
<value>红楼梦</value>
</array>
</property>
<!-- list注入-->
<property name="classmates">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
<value>田六</value>
</list>
</property>
<!-- Map注入-->
<property name="hobby">
<map>
<entry key="学生证" value="2018305086"/>
<entry key="身份证" value="123121231212123"/>
</map>
</property>
<!-- Set注入-->
<property name="cards">
<set>
<value>学生证</value>
<value>身份证</value>
<value>银行卡</value>
</set>
</property>
<!-- 空值注入-->
<property name="wife">
<null/>
</property>
<!-- properties-->
<property name="info">
<props>
<prop key="username">admin</prop>
<prop key="password">ftt123123</prop>
</props>
</property>
</bean>
打印数据
public class MyTest {
public static void main(String[] args) {
//获取ApplicationContext对象,拿到Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
/*
* Student{name='晚安世界',
* address=Address{address='重庆'},
* books=[水浒传, 西游记, 三国演义, 红楼梦],
* classmates=[张三, 李四, 王五, 田六],
* hobby={学生证=2018305086, 身份证=123121231212123},
* cards=[学生证, 身份证, 银行卡],
* wife='null', i
* nfo={password=ftt123123, username=admin}}
* */