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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.cdj.pojo.Address"/>
<bean id="student" class="com.cdj.pojo.Student">
<!--Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入,ref-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!--集合注入,ref-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="12312321312321312"/>
<entry key="银行卡" value="12312321312321312"/>
</map>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--property-->
<property name="info">
<props>
<prop key="id">123123213</prop>
<prop key="sex">male</prop>
</props>
</property>
</bean>
<!--p注入-->
<bean id="student2" class="com.cdj.pojo.Student" p:name="123"></bean>
<!-- c注入(构造器注入)-->
<bean id="user" class="com.cdj.pojo.User" c:age="123" c:name="hello" c:sex="nv"/>
</beans>
package com.cdj.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String [] books;
private List<String>hobbies;
private Map<String,String> card;
private Set<String>games;
private Properties info;
private String wife;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setInfo(Properties info) {
this.info = info;
}
public void setWife(String wife) {
this.wife = wife;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public String[] getBooks() {
return books;
}
public List<String> getHobbies() {
return hobbies;
}
public Map<String, String> getCard() {
return card;
}
public Set<String> getGames() {
return games;
}
public Properties getInfo() {
return info;
}
public String getWife() {
return wife;
}
@Override
public String toString() {
return "Hello{" +
"string='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}