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

Spring注入集合的值List,Set,Map,Properties

2016-03-11 15:26 495 查看
/**
*
*/
/**
* @author c
*
*/
package tju.chc.app.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import tju.chc.app.service.Axe;
import tju.chc.app.service.Person;

public class Chinese implements Person{
private Axe axe;
//下面是系列集合属性
private List<String> schools;
private Map<String,String> scores;
private Map<String, Axe> phaseAxes;
private Properties health;
private Set<Axe> axes;
private String[] books;

public Chinese(){
System.out.println("Spring 实例化主调bean:Chinese实例...");
}

public Chinese(Axe axe){
this.axe = axe;
}
//设置注入所需的setter方法
public void setAxe(Axe axe){
this.axe = axe;
}
//实现Person接口的useAxe方法
public void useAxe() {
//调用axe的chop()方法
//表明Person对象依赖于axe对象
System.out.println(axe.chop());
}
//schools属性依赖注入必需的setter方法
public void setSchools(List<String> schools) {
this.schools = schools;
}
//scores属性依赖注入必需的setter方法
public void setScores(Map<String,String> scores) {
this.scores = scores;
}
//phaseAxes属性依赖注入必需的setter方法
public void setPhaseAxes(Map<String, Axe> phaseAxes) {
this.phaseAxes = phaseAxes;
}
//health属性依赖注入必需的setter方法
public void setHealth(Properties health) {
this.health = health;
}
//axes属性依赖注入必需的setter方法
public void setAxes(Set<Axe> axes) {
this.axes = axes;
}
//books属性依赖注入的setter方法
public void setBooks(String[] books) {
this.books = books;
}

public void test(){
System.out.println(schools);
System.out.println(scores);
System.out.println(phaseAxes);
System.out.println(health);
System.out.println(axes);
System.out.println(java.util.Arrays.toString(books));
}

}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
<!-- 配置chinese实例,其实现类是Chinese -->
<bean id="chinese" class="tju.chc.app.service.impl.Chinese">
<!-- 将stoneAxe注入给axe属性 -->
<property name="axe" ref="steelAxe"/>
<property name="schools">
<!-- 为List属性配置属性值 -->
<list>
<!-- 每个value、ref、bean都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="scores">
<!-- 为Map属性配置属性值 -->
<map>
<!-- 为每个entry配置一个key-value对 -->
<entry key="数学" value = "87"/>
<entry key="英语" value = "89"/>
<entry key="语文" value = "82"/>
</map>
</property>

<property name="phaseAxes">
<!-- 为map属性配置属性值 -->
<map>
<!-- 每个entry配置一个Key-value对 -->
<entry key="原始社会" value-ref="stoneAxe"/>
<entry key="农业社会" value-ref="steelAxe"/>
</map>
</property>
<property name="health">
<!-- 为Properties属性配置属性值 -->
<!-- 每个prop元素配置一个属性项,其中key指定属性名 -->
<props>
<prop key="血压">正常</prop>
<prop key="身高">175</prop>

</props>
</property>
<property name="axes">
<!-- 为set属性配置属性值 -->
<set>
<!-- 为每个value、ref、bean都配置一个set元素 -->
<!-- <value>普通的字符串</value> -->
<bean class="tju.chc.app.service.impl.StoneAxe"/>
<ref local="steelAxe"/>
</set>
</property>
<property name="books">
<!-- 为数组属性配置属性值 -->
<list>
<!-- 每个value、ref、bean都配置一个数组元素 -->
<value>Struts 2权威指南</value>
<value>轻量级java ee企业应用实战</value>
<value>疯狂java讲义</value>
</list>

</property>
</bean>
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="tju.chc.app.service.impl.StoneAxe"/>
<!-- 将PersonService类部署成Spring容器中的Bean -->
<bean id="personService" class="tju.chc.app.service.PersonService">
<!-- 将wawa注入给name -->
<property name="name" value="wawa"/>
</bean>
<bean id="steelAxe" class="tju.chc.app.service.impl.SteelAxe"/>

</beans>
<!--
使用DTD作为语义约束的Spring配置文件如下
<?xml version="1.0" encoding="GBK"?>
指定Spring配置文件的DTD信息
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
Spring 配置文件的根元素
<beans>
...
<beans/>
-->
测试代码

package qi;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import tju.chc.app.service.Person;

public class BeanTest {
public static void main(String[] args){
//创建Spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean_constructure.xml");
//获取chinese实例,实现类是Chinese
Person p = ctx.getBean("chinese", Person.class);
p.useAxe();
p.test();
}
}


输出结果:

钢斧砍柴真快!

[小学, 中学, 大学]

{数学=87, 英语=89, 语文=82}

{原始社会=tju.chc.app.service.impl.StoneAxe@2e08db0b, 农业社会=tju.chc.app.service.impl.SteelAxe@7a79dbc7}

{血压=正常, 身高=175}

[tju.chc.app.service.impl.StoneAxe@5bfde7bf, tju.chc.app.service.impl.SteelAxe@7a79dbc7]

[Struts 2权威指南, 轻量级java ee企业应用实战, 疯狂java讲义]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: