Spring容器中,对于Bean的属性,或者说是集合,可以使用Spring容器进行值的注入和加载。包括基本类型的值的注入和容器类的注入。首先需要写一个Bean.
package com.bird.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.Map.Entry; import java.util.Properties; import java.util.Set; import com.bird.dao.PersonDao; public class PersonServerBean { private PersonDao personDao; private String name; private Set<String> set = new HashSet<String>(); private List<String> list = new ArrayList<String>(); private Properties properties = new Properties(); private Map<String,String> map = new HashMap<String,String>(); public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public String getName() { return name; } public void setName(String name) { this.name = name; } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void init() { personDao.add(); System.out.println(name); for(String value : set){ System.out.println(value); } for(String temp:list){ System.out.println(temp); } for(int i = 1; i <= properties.size(); i++){ System.out.println(properties.getProperty(String.valueOf(i))); } for(Entry<String, String> entry :map.entrySet()){ System.out.println(entry.getKey()+"=="+entry.getValue()); } } }
在Bean.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <context:annotation-config/> <!-- <bean id="personDao" class="com.bird.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="com.bird.service.impl.PersonServerImpl" init-method="init" destroy-method="destory" scope="prototype"></bean> <bean id="personServerBean" class="com.bird.service.impl.PersonServerBean"> <property name="personDao" ref="personDao"></property> </bean> --> <bean id="personServerBean" class="com.bird.service.impl.PersonServerBean"> <property name="personDao"> <bean class="com.bird.dao.impl.PersonDaoBean"></bean> </property> <property name="name" value="bird"></property> <property name="set"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="list"> <list> <value>one</value> <value>two</value> <value>three</value> </list> </property> <property name="properties"> <props> <prop key="1">Properties1</prop> <prop key="2">Properties2</prop> <prop key="3">Properties3</prop> </props> </property> <property name="map"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> </map> </property> </bean> </beans>
这样就可以完成对容器类的属性注入了