spring--集合注入(常规方法)

摘要:
数据、列表、集合、映射和属性被注入packageSpring_ collections;/***创建dbyluozhitaoion2017/8/11.*/publicclassEmployee{publicintgetAge(){returnage;}publicvoisetAge(intage){this.age=age;}publicStringgetName(

数据,list,set,map,Properties 集合注入

package Spring_collections;

/**
 * Created by luozhitao on 2017/8/11.
 */
public class Employee {

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    private int age;
    private  String name;


    public Employee(int age, String name) {
        this.age = age;
        this.name = name;
    }

  public Employee(){}


}
package Spring_collections;

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

/**
 * Created by luozhitao on 2017/8/11.
 */
public class Employee_collection {

    public String[] getEmpName() {
        return empName;
    }

    public void setEmpName(String[] empName) {
        this.empName = empName;
    }

    public List<Employee> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Employee> empList) {
        this.empList = empList;
    }

    public Set<Employee> getEmpSets() {
        return empSets;
    }

    public void setEmpSets(Set<Employee> empSets) {
        this.empSets = empSets;
    }

    public Map<String, Employee> getEmpMaps() {
        return empMaps;
    }

    public void setEmpMaps(Map<String, Employee> empMaps) {
        this.empMaps = empMaps;
    }

    public Properties getPp() {
        return pp;
    }

    public void setPp(Properties pp) {
        this.pp = pp;
    }

    private String [] empName;
    private List<Employee> empList;
    private Set<Employee> empSets;
    private Map<String,Employee> empMaps;
    private Properties pp;




}
package Spring_collections;

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

import java.util.Iterator;
import java.util.Map;

/**
 * Created by luozhitao on 2017/8/11.
 */
public class Emp_app {

    public static void main(String [] args){


        ApplicationContext context=new ClassPathXmlApplicationContext("collection_beans.xml");

        Employee_collection employee_collection=(Employee_collection)context.getBean("collection_emp");


        //数组
        for (String str:employee_collection.getEmpName()){

            System.out.println("集合 "+str);

        }

        System.out.println("----------------");


        //list
        for(Employee emp:employee_collection.getEmpList()){


            System.out.println("list"+emp.getName()+"--"+emp.getAge());


        }

        System.out.println("----------------");

        //set

        for(Employee emp:employee_collection.getEmpSets()){


            System.out.println("set"+emp.getName()+"--"+emp.getAge());

        }



        System.out.println("----------------");
        //map

         Map<String,Employee> map=employee_collection.getEmpMaps();
       for (Map.Entry<String,Employee> m:employee_collection.getEmpMaps().entrySet()){

           System.out.println("map"+m.getKey()+":"+m.getValue().getName());

       }

        //迭代器方式
        System.out.println("map迭代器方式----------------");
        Iterator<String> iterator=employee_collection.getEmpMaps().keySet().iterator();
        while (iterator.hasNext()){

            Employee e=map.get(iterator.next());
            System.out.println("maps迭代器"+e.getName()+"--"+e.getAge());

        }



        System.out.println("----------------");

        //properties


        for(Map.Entry<Object,Object> en:employee_collection.getPp().entrySet()){


            System.out.println("properties"+en.getKey().toString()+":"+en.getValue().toString());
        }



    }

}

bean.xml

<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"
       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">

    <context:component-scan base-package="spring_service" />


    <bean   class="Spring_collections.Employee">
        <property name="age" value="15"></property>
        <property name="name" value="cat"></property>
    </bean>

    <bean   class="Spring_collections.Employee">
        <property name="age" value="16"></property>
        <property name="name" value="cat1"></property>
    </bean>

    <bean   class="Spring_collections.Employee_collection">

        <!-- 数组 -->
        <property name="empName">
            <list>
                <value>猫儿爹</value>
                <value>猫儿妈</value>
                <value>猫儿</value>
            </list>
        </property>

       <!-- list -->
        <property name="empList">
            <list>

                <ref bean="emp1"></ref>
                <ref bean="emp2"></ref>
            </list>
        </property>


        <!--  sets  -->
        <property name="empSets">
            <set>
              <ref bean="emp1"></ref>
                <ref bean="emp2"></ref>
            </set>

        </property>

       <!-- maps -->
        <property name="empMaps">
            <map>
                <entry key="001" value-ref="emp1"></entry>
                <entry key="002" value-ref="emp2"></entry>

            </map>
        </property>

        <!-- properties  -->
        <property name="pp">
        <props>
          <prop key="003">hello3</prop>
          <prop key="004">hello4</prop>

        </props>

        </property>

    </bean>




</beans>

免责声明:文章转载自《spring--集合注入(常规方法)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇c#生成cad缩略图或者图片Npoi XWPF Word 导出时插入图片无法显示下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Java 并发系列之十:java 并发框架(2个)

108maximumPoolSize109线程池中允许的最大线程数。线程池的阻塞队列满了之后,如果还有任务提交,如果当前的线程数小于maximumPoolSize,则会新建线程来执行任务。线程的创建和销毁是需要代价的。121threadFactory122用于设置创建线程的工厂。所谓拒绝策略,是指将任务添加到线程池中时,线程池拒绝该任务所采取的相应策略。...

如何下载Chrome离线版EXE安装文件和MSI版安装文件

对于Chrome的稳定版本(官方版本),您只需添加“?”在Chrome的“最终用户许可协议”页面上的链接之后?Standalone=1对于Beta版和开发版Chrome,只需记住以下地址:http://dl.google.com/chrome/install/{versionnumber}/crome_安装程序中的版本号。exe表示要下载的Chrome版本号...

scan chain的原理和实现——5.UDTP

UDTP(用户定义的测试点)指示DFTC在设计中用户指定的位置插入控制点和观察点。1.为什么使用UDTP?修复不可控的时钟和/或异步输入;增加设计的测试覆盖率;减少模式数量2.UDTP类型① 力0、力1、力01、力z0、力z1、力z01②控制_ 0...

批处理bat脚本自动配置java的jdk环境变量

前言每次更换计算机或重新安装系统时,都需要重新配置java系统路径。但我不想每次都检查配置方法,所以我编写了一个脚本来自动配置。脚本内容@echooff@echo步骤1:输入要设置的JAVA_HOME路径:set/pinput=“请输入JAVA_HOME路径:”@echo步骤2:设置JAVA_ HOME路径setxJAVA_HOME“%input%”/M@e...

安装pygame

在python3中安装pygame库。一段时间后,您可以看到安装成功,并且可以导入pygame...

easyExcel自动合并单元格

importcom.alibaba.excel.write.handler.CellWriteHandler;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.util.CellRangeAddress;int[]mergeColumnIndex){this.mergeRowInd...