Spring中数组、List、Set数据的注入

<?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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
 
    <bean id="person" class="com.amaker.spring.Person">
        <!--数组的注入  -->
        <property name="names">
            <list>
                <value>aa</value>
                <value>bb</value>
                <value>cc</value>
                <value>dd</value>
            </list>
        </property>
        
        <!--list的注入  -->
        <property name="studentList">
            <list>
                <ref bean="student"/>
                <ref bean="student1"/>
            </list>
        </property>
        
        <!--Set的注入  -->
        <property name="studentSet">
            <set>
                <ref bean="student"/>
                <ref bean="student1"/>
                <ref bean="student1"/>
                <ref bean="student1"/>
                <ref bean="student1"/>
            </set>
        </property>
        
        <!--Map的注入  -->
        <property name="studentMap">
            <map>
                <entry key="11" value-ref="student"></entry>
                <entry key="22" value-ref="student1"></entry>
            </map>
        </property>
        
        <!--Properties的注入  -->
        <property name="pp">
            <props>
                <prop key="key1">aaaaaaaaaaaaaaaaaaa</prop>
                <prop key="key2">bbbbbbbbbbbbbbbbbbb</prop>
            </props>
        </property>
        
        

    </bean>
    
    
    <bean id="student" class="com.amaker.spring.Student">
        <property name="name">
            <value>小明</value>
        </property>
    </bean>
    
    <bean id="student1" class="com.amaker.spring.Student">
        <property name="name">
            <value>大明</value>
        </property>
    </bean>
    
    
    
</beans>









<pre name="code" class="java">package com.amaker.spring;

public class Student {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}



package com.amaker.spring;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class Person {private String []names;private List<Student> studentList;private Set<Student> studentSet;private Map<String,Student> studentMap;private Properties pp;public Properties getPp() {return pp;}public void setPp(Properties pp) {this.pp = pp;}public Map<String, Student> getStudentMap() {return studentMap;}public void setStudentMap(Map<String, Student> studentMap) {this.studentMap = studentMap;}public Set<Student> getStudentSet() {return studentSet;}public void setStudentSet(Set<Student> studentSet) {this.studentSet = studentSet;}public List<Student> getStudentList() {return studentList;}public void setStudentList(List<Student> studentList) {this.studentList = studentList;}public String[] getNames() {return names;}public void setNames(String[] names) {this.names = names;}}
 

package com.amaker.spring;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

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

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)ac.getBean("person");
		
		//获取注入的数组
		for(String name:person.getNames()){
			System.out.println(name);
		}
		//获取注入的集合List
		for(Student student:person.getStudentList()){
			System.out.println(student.getName());
		}
		//获取注入的集合Set
		for(Student student:person.getStudentSet()){
			System.out.println(student.getName());
		}
		
		
		//获取注入的Map数据,两种获取方式
		//1,迭代获取方式:
		Map<String,Student> studentMap = person.getStudentMap();
		Iterator it = studentMap.keySet().iterator();
		while(it.hasNext()){
			String key = (String) it.next();
			Student s = studentMap.get(key);
			System.out.println("key= " +key + "  "+s.getName());
		}

		//2,entry
		for(Entry<String, Student> entry:person.getStudentMap().entrySet()){
			System.out.println(entry.getKey()+"  "+entry.getValue().getName());
		}
		
		
		//获取注入的属性数据
		//第一种获取方式
		Properties pp = person.getPp();
		for(Entry<Object, Object> entry:pp.entrySet()){
			System.out.println(entry.getKey() + "   "+entry.getValue());
		}
		//第二种获取方式
		Enumeration en = pp.keys();
		while(en.hasMoreElements()){
			String key = (String) en.nextElement();
			System.out.println(key + "   "+pp.getProperty(key));
		}

	}

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值