定义接口:
定义实现类:
Spring配置文件:
运行结果:
收到:Hello,Spring!
List --> [小学, 中学, 大学]
Map --> {数学=88, 语文=99}
Properties --> [字符串斧子, 重写toString, 重写toString]
Set --> {血压=正常, 身高=178}
package Bean.collections;
public interface Person {
public void useAxe();
}
package Bean.collections;
public interface Axe {
public String chop();
}
定义实现类:
package Bean.collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import Bean.collections.Person;
public class Chinese implements Person {
private List schools = new ArrayList();
private Map scores = new HashMap();
private Properties health = new Properties();
private Set axes = new HashSet();
public Set getAxes() {
return axes;
}
public void setAxes(Set axes) {
this.axes = axes;
}
public Properties getHealth() {
return health;
}
public void setHealth(Properties health) {
this.health = health;
}
public List getSchools() {
return schools;
}
public void setSchools(List schools) {
this.schools = schools;
}
public Map getScores() {
return scores;
}
public void setScores(Map scores) {
this.scores = scores;
}
public void useAxe() {
System.out.println("List --> " + schools);
System.out.println("Map --> " + scores);
System.out.println("Properties --> " + axes);
System.out.println("Set --> " + health);
}
}
package Bean.collections;
public class WoodAxe implements Axe {
public WoodAxe(String arg)
{
System.out.println("收到:"+arg);
}
public String chop() {
return "这是一把木头斧子";
}
public String toString() {
// TODO Auto-generated method stub
return "重写toString";
}
}
package Bean.collections;
public class SteelAxe implements Axe {
public String chop() {
return "这是一把铁斧子";
}
public String toString(){
return "重写toString";
}
}
Spring配置文件:
<?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-2.5.xsd">
<bean id="chinese" class="Bean.collections.Chinese">
<property name="schools">
<list>
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="health">
<props>
<prop key="血压">正常</prop>
<prop key="身高">178</prop>
</props>
</property>
<property name="scores">
<map>
<entry key="数学">
<value>88</value>
</entry>
<entry key="语文">
<value>99</value>
</entry>
</map>
</property>
<property name="axes">
<set>
<value>字符串斧子</value>
<!-- 用嵌套bean定义属性 -->
<bean class="Bean.collections.WoodAxe" >
<constructor-arg index="0">
<value>Hello,Spring!</value>
</constructor-arg>
</bean>
<!-- 引用bean作为属性 -->
<ref local="steelaxe" />
</set>
</property>
</bean>
<bean id="steelaxe" class="Bean.collections.SteelAxe"></bean>
</beans>
运行结果:
收到:Hello,Spring!
List --> [小学, 中学, 大学]
Map --> {数学=88, 语文=99}
Properties --> [字符串斧子, 重写toString, 重写toString]
Set --> {血压=正常, 身高=178}