首页 > 代码库 > (二)装配各种集合类型的属性
(二)装配各种集合类型的属性
一、接口PersonService,复制get方法,便于得到相应集合
public interface PersonService { public void save(); public Set<String> getSets(); public List<String> getLists(); public Properties getProperties(); public Map<String, String> getMaps(); }
二、子类PersonServiceImpl
public class PersonServiceImpl implements PersonService { 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 Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } 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; } }
三、applicationContext.xml的配置
<bean id="personService" class="com.lovo.u34.service.impl.PersonServiceImpl"> <!-- set集合 --> <property name="sets"> <set> <value>set第一个</value> <value>set第二个</value> <value>set第三个</value> </set> </property> <!-- list集合 --> <property name="lists"> <list> <value>list01</value> <value>list02</value> <value>list03</value> </list> </property> <!-- properties --> <property name="properties"> <props> <prop key="key1">pro1</prop> <prop key="key2">pro2</prop> <prop key="key3">pro3</prop> </props> </property> <!-- map --> <property name="maps"> <map> <entry key="key-1" value="http://www.mamicode.com/key1"></entry> <entry key="key-2" value="http://www.mamicode.com/key2"></entry> <entry key="key-3" value="http://www.mamicode.com/key3"></entry> </map> </property> </bean> </beans>
四、Test中打印
public class Test extends TestCase{ public void testSave(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonService ps=(PersonService) ac.getBean("personService"); for(String value:ps.getSets()){ System.out.println(value); } for(String value:ps.getLists()){ System.out.println(value); } for(Object value:ps.getProperties().keySet()){ System.out.println(ps.getProperties().getProperty((String)value)); } for(String value:ps.getMaps().keySet()){ System.out.println(ps.getMaps().get(value)); } } }
(二)装配各种集合类型的属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。