第9讲 --Spring如何装配各种集合类型的属性

 

 集合类型的装配

 

 private Set<String> sets = new HashSet<String>();

 


 private List<String> lists = new ArrayList<String>();


 private Properties properties = new Properties();


 private Map<String, String> maps = new HashMap<String, String>();


        ....//这里省略属性的getter和setter方法

 

 

步骤:

 

(1).修改PersionServiceBean.java ,给它加上一些集合类属性

 

package cn.com.xinli.service.impl;

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 org.apache.log4j.Logger;

import cn.com.xinli.dao.PersionDao;
import cn.com.xinli.service.PersionSevice;
										   
public class PersionServiceBean implements PersionSevice 
{
	Logger log=Logger.getLogger(PersionServiceBean.class);
	private PersionDao persionDao;
	private String name;
	private int age;
	
	private Set<String> sets = new HashSet<String>();
	private List<String> lists = new ArrayList<String>();
	private Properties properties = new Properties();
	private Map<String, String> maps = new HashMap<String, String>();
	
	
	public Set<String> getSets()
	{
		return sets;
	}
	public void setSets(Set<String> sets)
	{
		this.sets = sets;
	}
	public List<String> getLists()
	{
		return lists;
	}
	public void setLists(List<String> lists)
	{
		this.lists = lists;
	}
	public Properties getProperties()
	{
		return properties;
	}
	public void setProperties(Properties properties)
	{
		this.properties = properties;
	}
	public Map<String, String> getMaps()
	{
		return maps;
	}
	public void setMaps(Map<String, String> maps)
	{
		this.maps = maps;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public PersionDao getPersionDao()
	{
		return persionDao;
	}
	public void setPersionDao(PersionDao persionDao)
	{
		this.persionDao = persionDao;
	}
	public void init()
	{
		log.info("初始化资源");
	}
	public PersionServiceBean() 
	{
		log.info("我被实例化了");
	}

	public void save()
	{
		log.info("name:"+name);
		log.info("age:"+age);
		 this.persionDao.add();
	}
	public void destory()
	{
		log.info("释放资源");
	}
	
}	

 

 

(2) 配置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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	        
	<bean id="persionDao" class="cn.com.xinli.dao.impl.PersionDaoBean"></bean>                                                               
	<bean id="persionServiceBean" class="cn.com.xinli.service.impl.PersionServiceBean" init-method="init" destroy-method="destory">
		<property name="persionDao" ref="persionDao"/>
		<property name="name" value="huxl"/>
		<property name="age" value="26"/>
			<property name="sets">
          		<set>
          			<value>第一个</value>
          			<value>第二个</value>
          			<value>第三个</value>
          		</set>
          	</property>
          	<property name="lists">
          		<list>
          			<value>第一个list元素</value>
          			<value>第二个list元素</value>
          			<value>第三个list元素</value>
          		</list>
          	</property>
          	<property name="properties">
          		<props>
          			<prop key="key1">value1</prop>
          			<prop key="key2">value2</prop>
          			<prop key="key3">value3</prop>
          		</props>
          	</property>
          	<property name="maps">
          		<map>
          			<entry key="key-1" value="value-1"/>
          			<entry key="key-2" value="value-2"/>
          			<entry key="key-3" value="value-3"/>
          		</map>
          	</property>
	</bean>
	
	
	<!-- 
    <bean id="persionServiceBean2" class="cn.com.xinli.service.impl.PersionServiceBeanFactory" factory-method="createPersionServiceBean"/>
    
    <bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
    <bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
     -->
</beans>

 

 

(3) 测试:

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean");
		ps.save();
		System.out.println("========set===========");
		for(String value : ps.getSets()){
			System.out.println(value);
		}
		System.out.println("========list===========");
		for(String value : ps.getLists()){
			System.out.println(value);
		}
		System.out.println("========properties===========");
		for(Object key : ps.getProperties().keySet()){
			System.out.println(key+"="+ ps.getProperties().getProperty((String)key));
		}
		System.out.println("========map===========");
		for(String key : ps.getMaps().keySet()){
			System.out.println(key+"="+ ps.getMaps().get(key));
		}

 

 

 (5) 结果

 

2009-05-30 17:52:17,906  INFO (PersionServiceBean.java:91) - 我被实例化了
2009-05-30 17:52:18,046  INFO (PersionServiceBean.java:87) - 初始化资源
2009-05-30 17:52:18,062  INFO (PersionServiceBean.java:96) - name:huxl
2009-05-30 17:52:18,062  INFO (PersionServiceBean.java:97) - age:26
2009-05-30 17:52:18,062  INFO (PersionDaoBean.java:15) - 执行了PersionDaoBean中的add()方法
========set===========
第一个
第二个
第三个
========list===========
第一个list元素
第二个list元素
第三个list元素
========properties===========
key3=value3
key2=value2
key1=value1
========map===========
key-1=value-1
key-2=value-2
key-3=value-3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值