装配各种集合类型的属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装配各种集合类型的属性相关的知识,希望对你有一定的参考价值。

一、接口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="key1"></entry>
       			<entry key="key-2" value="key2"></entry>
       			<entry key="key-3" value="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));
		}
	}
}

  

  

  

  

以上是关于装配各种集合类型的属性的主要内容,如果未能解决你的问题,请参考以下文章

spring装配集合

[Spring实战系列]装配集合

Java Spring5之IOC容器

Spring中基于XML的自动装配(自动赋值)

集合装配

Spring之自动装配