Spring 注入 List Set Properties Map

要抓住主要矛盾,最主要的配置如下:

<bean id="customerBean" class="com.myapp.core.collection.Customer">

     <!-- list example   -->
       <property name="lists">
          <list>
            <value>3</value>
            <ref  bean="personBean"/>
            <bean name="otherBean" class="com.myapp.core.collection.Person">
             <property name="name" value="myList"/>
             <property name="address" value="myaddress" />
             <property name="age" value="24" />
            </bean>
          </list>
       </property>
      
      <!-- set  example  demo -->
      <property name="sets">
       <set>
            <value>3</value>
            <ref  bean="personBean"/>
            <bean name="otherBean" class="com.myapp.core.collection.Person">
             <property name="name" value="myList"/>
             <property name="address" value="myaddress" />
             <property name="age" value="24" />
            </bean>
       </set>
      </property>
    
    <!--  map  example  demo -->
    
      <property name="maps">
       <map>
         <entry key="example1" value="love1"/>
         <entry key="example2" value-ref="personBean" />
         <entry key="example3">
          <bean name="otherBean" class="com.myapp.core.collection.Person">
             <property name="name" value="myList"/>
             <property name="address" value="myaddress" />
             <property name="age" value="24" />
            </bean>
         </entry>
       </map>
      </property>
      
      <property name="pros">
        <props>
         <prop key="key1">myexample--value1</prop>
        <prop key="key2">myexample--value2</prop>
        </props>
      
      </property>
   
   
   </bean>
   
   <bean id="personBean" class="com.myapp.core.collection.Person">
     <property name="name"  value="top"/>
     <property name="address" value="beijing"></property>
     <property name="age" value="1"></property>
   </bean>
   

以上分别在customerBean中注入了 List  Set  Map  Properties

引用的person类如下:

package com.myapp.core.collection;

public class Person {
  private String name;
  private String  address;
  private int  age;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getAddress() {
	return address;
}
public void setAddress(String address) {
	this.address = address;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}

public  String  toString(){
	
 return  "[ name:  "+name + "  address: "+address+"  age:"+age+"]";
}
}

Customer类如下:其中采用循环输入对应的collection中的元素;

package com.myapp.core.collection;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Customer {
	private List<Object> lists;
	private Set<Object>  sets;
	private Map<Object,Object>  maps;
	private Properties  pros;
	public void setLists(List<Object> lists) {
		this.lists = lists;
	}
	public void setSets(Set<Object> sets) {
		this.sets = sets;
	}
	public void setMaps(Map<Object, Object> maps) {
		this.maps = maps;
	}
	public void setPros(Properties pros) {
		this.pros = pros;
	}
	
	public  String   toString(){
		System.out.print("[");
		for(int i=0;i<lists.size();i++){
			System.out.print(lists.get(i));
		}
		System.out.println("]");
		
		System.out.print("[");
			System.out.print(sets.toString());
		System.out.println("]");
		
		System.out.print("[");
		
	    Set<Object> set2 =	maps.keySet();
	   // System.out.println(set2.size());
	    for(Iterator<Object>  i =set2.iterator();i.hasNext();){
	    	   String   next = i.next().toString();
			System.out.print(next+":"+maps.get(next).toString());
		}
		System.out.println("]");
		
		System.out.println("["+pros+"]");
		return  "";
	}

}

 编写测试类:MainTest.java

package com.myapp.core.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
   public static void main(String[] args) {
	
	   ApplicationContext   context = new  ClassPathXmlApplicationContext("SpringBeans.xml");
	   
	   Customer  customer = (Customer)context.getBean("customerBean");
	   
	     System.out.println(customer.toString());
   }
}

运行结果如下:

[3[ name:  top  address: beijing  age:1][ name:  myList  address: myaddress  age:24]]
[[3, [ name:  top  address: beijing  age:1], [ name:  myList  address: myaddress  age:24]]]
[example1:love1example2:[ name:  top  address: beijing  age:1]example3:[ name:  myList  address: myaddress  age:24]]
[{key2=myexample--value2, key1=myexample--value1}]



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值