您的位置:首页 > 编程语言 > Java开发

List,Set,Map,Properties的Spring注入实例

2007-04-06 10:30 447 查看
定义接口:


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(schools);


System.out.println(scores);


System.out.println(axes);


System.out.println(health);


}




}




package Bean.collections;






public class SteelAxe implements Axe ...{






public String chop() ...{


return "这是一把铁斧子";




}




}






package Bean.collections;






public class WoodAxe implements Axe ...{






public String chop() ...{


return "这是一把木头斧子";




}




}



spring配置文件:


<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">




<beans>




<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"/>


<!-- 引用bean作为属性 -->


<ref local="steelaxe"/>


</set>


</property>


</bean>


<bean id="steelaxe" class="Bean.collections.SteelAxe"></bean>


</beans>

测试代码:




public static void main(String[] args) throws Exception ...{




String path=new Test().getClass().getResource("/").getPath();


String realpath=path.substring(1, path.length());


ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/collection.xml");






Person person1=(Person)context.getBean("chinese");


person1.useAxe();






}

运行结果“

[小学, 中学, 大学]
{数学=88, 语文=99}
[字符串斧子, Bean.collections.WoodAxe@1506dc4, Bean.collections.SteelAxe@1126b07]
{血压=正常, 身高=178}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: