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

来源:互联网 发布:购买的淘宝店铺安全吗 编辑:程序博客网 时间:2024/05/29 13:24
<?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);}//获取注入的集合Listfor(Student student:person.getStudentList()){System.out.println(student.getName());}//获取注入的集合Setfor(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,entryfor(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
原创粉丝点击