Spring属性注入set方式注入集合

这个是被注入的类
package Collection;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class collections {

    private ArrayList<String> ayyay;

    public void setAyyay(ArrayList<String> ayyay) {
        this.ayyay = ayyay;
    }
    private Set<String> sets;
    private Map<Integer,String> maps;
    private Properties  pre;

    public Map<Integer, String> getMaps() {
        return maps;
    }

    public void setMaps(Map<Integer, String> maps) {
        this.maps = maps;
    }

    public ArrayList<String> getAyyay() {
        return ayyay;
    }

    public Properties getPre() {
        return pre;
    }

    public void setPre(Properties pre) {
        this.pre = pre;
    }

    public Set<String> getSets() {
        return sets;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
}

xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="collections" class="Collection.collections" scope="singleton">
        <property name="sets">
             <set>
                 <value>老孔</value>
                 <value>老大</value>
                 <value>老六</value>
                 <value>老七</value>
                 <value>老八</value>
                 <value>老九</value>

             </set>
        </property>
        <property name="ayyay">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
                <value>4</value>
            </list>
        </property>
        <property name="maps">
              <map>
                  <entry key="1" value="01"></entry>
                  <entry key="2" value="02"></entry>
                  <entry key="3" value="03"></entry>
                  <entry key="4" value="04"></entry>
              </map>
        </property>
        <property name="pre">
            <props>
                <prop key="1">jdbc1</prop>
                <prop key="2">jdbc2</prop>
                <prop key="3">jdbc3</prop>
                <prop key="4">jdbc4</prop>
            </props>
        </property>
    </bean>





</beans>

spring的测试文件为

import Collection.collections;
import Dao.UserDao1;
import Dao.UserDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JavaTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml","spring-service.xml");
        UserService userService=(UserService) applicationContext.getBean("userService");
        userService.save();
        ApplicationContext applicationContext2=new ClassPathXmlApplicationContext("collections.xml");
        collections  coll=(collections)applicationContext2.getBean("collections");
        List<String> list=coll.getAyyay();
        for (String s:list) {
            System.out.println(s);

        }
        Set<String> se=coll.getSets();
        Iterator<String> iterator =se.iterator();
       while(iterator.hasNext())
       {
           System.out.println(iterator.next());

       }
        Map<Integer,String> maps=coll.getMaps();
       Iterator<Integer>  iterator1=maps.keySet().iterator();
      while(iterator1.hasNext())
      {
          Integer integer=iterator1.next();
          String name=maps.get(integer);
          System.out.println(name+ "     "+integer);
      }

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42369687/article/details/88975022
今日推荐