spring对于集合类型的装配

来源:互联网 发布:杨钰莹偶像来了 知乎 编辑:程序博客网 时间:2024/05/14 17:50
package blog.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 blog.dao.PersonDao;import blog.service.PersonService;public class PersonServiceBean implements PersonService {private PersonDao personDao;private String id;private String name;private Map<String, String> maps = new HashMap<String, String>();private List<String> list = new ArrayList<String>();private Properties properties = new Properties();private Set<String> set = new HashSet<String>();public Map<String, String> getMaps() {return maps;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}public Set<String> getSet() {return set;}public void setSet(Set<String> set) {this.set = set;}public String getId() {return id;}public void setId(String id) {this.id = id;}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(){System.out.println("in init method......");}public PersonServiceBean(){System.out.println("in PersonServiceBean'constructor method!");}public void save(){System.out.println("id = " + id + "\tname=" + name);personDao.save();}public void destroy(){System.out.println("in destroy method......");}}



package blog.service;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public interface PersonService {public abstract void save();public Map<String, String> getMaps() ;public List<String> getList();public Properties getProperties();public Set<String> getSet();}


<?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"           default-lazy-init="false">                      <!-- id属于xml所有的属性,是确定bean唯一的属性,不能包含特殊字符   建议第一个字母小写 -->           <!-- name属性可以包含特殊字符,当不包含特殊字符时应选用id来表示bean,id可以被xml解析 -->                        <!-- 使用类构造器方法创建bean -->                     <bean id="personDao" class="blog.dao.impl.PersonDaoImpl"></bean>                             <bean id="personService" class="blog.service.impl.PersonServiceBean" >          <property name="personDao" ref="personDao"></property>                    <!--<property name="personDao">          <bean class="blog.dao.impl.PersonDaoImpl"></bean>          </property>          --><property name="id" value="11"></property>          <property name="name" value="xxxx"></property>          <property name="maps">          <map>          <entry key="map-key1" value="map-value1"></entry>          <entry key="map-key2" value="map-value2"></entry>          <entry key="map-key3" value="map-value3"></entry>          </map>          </property>          <property name="list">          <list>          <value>list1</value>          <value>list2</value>          <value>list3</value>          </list>          </property>          <property name="properties">          <props>          <prop key="properties-key1">properties-value1</prop>          <prop key="properties-key2">properties-value2</prop>          <prop key="properties-key3">properties-value3</prop>          </props>          </property>          <property name="set">          <set>          <value>set-value1</value>          <value>set-value2</value>          <value>set-value3</value>          </set>          </property>          </bean>                     </beans>


package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import blog.service.PersonService;import blog.service.impl.PersonServiceBean;public class springtest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Testpublic void instanceSpring(){//实例化spring容器//使用:在类路径寻找配置文件来实例化容器//BlogClassPathXMLApplicationContext ctx = new BlogClassPathXMLApplicationContext("beans.xml");ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//ctx.close();PersonService personService = (PersonService)ctx.getBean("personService");personService.save();System.out.println("========list========");for(String list : personService.getList()){System.out.println(list);}System.out.println("========map========");for(String key : personService.getMaps().keySet()){System.out.println("key = " + key + " value=" + personService.getMaps().get(key));}System.out.println("========Properties========");for(Object key : personService.getProperties().keySet()){System.out.println("key = " + key + " value=" + personService.getProperties().get(key));}System.out.println("========set========");for(String set : personService.getSet()){System.out.println(set);}}}


原创粉丝点击