java
package cn.ioc.student; import java.util.List; import java.util.Map; import java.util.Set; public class Student { private String name; private Address addr; private String[] books; private List<String> testlist; private Map<String,String> cards; private Set<String> games; private String wife; public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public List<String> getTestlist() { return testlist; } public void setTestlist(List<String> testlist) { this.testlist = testlist; } public Map<String, String> getCards() { return cards; } public void setCards(Map<String, String> cards) { this.cards = cards; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } //------------------------------------------------- public Address getAddr() { return addr; } public void setAddr(Address addr) { this.addr = addr; } public String getName() { return name; } public void setName(String name) { this.name = name; } //show public void show(){ System.out.print("---我叫"+name+",---我住在"+addr.getAddr()+"---我爱看的书有"); for(int i=0;i<books.length;i++){ System.out.print(books[i]+","); } System.out.println("---list"+testlist); System.out.println("---map="+cards); System.out.println("---set="+games); System.out.println("---wife老婆="); } }
address.class
package cn.ioc.student; public class Address { private String addr; public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }
beans.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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ioc --> <bean id="address" class="cn.ioc.student.Address"> <property name="addr" value="beijing"></property> </bean> <!-- ref引用注入 --> <bean id="student" class="cn.ioc.student.Student"> <property name="name" value="shangren"></property> <property name="addr" ref="address"></property> <!-- 数组注入 --> <property name="books"> <array> <value>书1</value> <value>书2</value> <value>书3</value> </array> </property> <!-- list注入 --> <property name="testlist"> <list> <value>排球</value> <value>足球</value> <value>篮球</value> </list> </property> <!-- map --> <property name="cards"> <map> <entry key="中国银行" value="1997081100"></entry> <entry> <key><value>建设银行</value></key> <value>110110110</value> </entry> </map> </property> <!-- set --> <property name="games"> <set> <value>dota</value> <value>cs</value> <value>cs</value> </set> </property> <!-- 空 null --> <property name="wife"> <null></null> </property> </bean> </beans>
test.class
package cn.ioc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.ioc.student.Student; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Student stu = (Student) ac.getBean("student"); stu.show(); } }
结果