zoukankan      html  css  js  c++  java
  • Spring配置文件中注入复杂类型属性

    Spring在整合其他框架时在配置文件中可能会涉及到复杂类型属性的注入,以下举例说明:

    1.数组类型

    2.List集合类型

    3.Set集合类型

    4.Map集合类型

    5.Properties属性类型

    直接上代码:

    实体类:CollectionBean.java

    package com.imooc.ioc.demo5;
    
    import java.util.*;
    
    /**
     * Spring复杂类型的集合注入
     */
    public class CollectionBean {
        private String[] arrs;  //数组类型
    
        private List<String> list; //list类型
    
        private Set<String> set; //set类型
    
        private Map<String , Integer> map; //map类型
    
        private Properties properties; //属性类型
    
        public String[] getArrs() {
            return arrs;
        }
    
        public void setArrs(String[] arrs) {
            this.arrs = arrs;
        }
    
        public List<String> getList() {
            return list;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        public Set<String> getSet() {
            return set;
        }
    
        public void setSet(Set<String> set) {
            this.set = set;
        }
    
        public Map<String, Integer> getMap() {
            return map;
        }
    
        public void setMap(Map<String, Integer> map) {
            this.map = map;
        }
    
        public Properties getProperties() {
            return properties;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        @Override
        public String toString() {
            return "CollectionBean{" +
                    "arrs=" + Arrays.toString(arrs) +
                    ", list=" + list +
                    ", set=" + set +
                    ", map=" + map +
                    ", properties=" + properties +
                    '}';
        }
    }

    配置文件:applicationContext.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="collectionBean" class="com.imooc.ioc.demo5.CollectionBean">
            <!--数组类型注入-->
            <property name="arrs">
                <list>
                    <value>111</value>
                    <value>222</value>
                    <value>333</value>
                </list>
            </property>
    
            <!--list类型注入-->
            <property name="list">
                <list>
                    <value>aaa</value>
                    <value>bbb</value>
                    <value>ccc</value>
                </list>
            </property>
    
            <!--set类型注入-->
            <property name="set">
                <set>
                    <value>ddd</value>
                    <value>eee</value>
                    <value>fff</value>
                </set>
            </property>
    
            <!--map类型注入-->
            <property name="map">
                <map>
                    <entry key="aaa" value="111"></entry>
                    <entry key="bbb" value="222"></entry>
                    <entry key="ccc" value="333"></entry>
                </map>
            </property>
    
            <!--properties类型注入-->
            <property name="properties">
                <props>
                    <prop key="aaa">555</prop>
                    <prop key="bbb">666</prop>
                    <prop key="ccc">777</prop>
                </props>
            </property>
        </bean>
    </beans>

    测试类:SpringDemo5.java

    package com.imooc.ioc.demo5;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringDemo5 {
        @Test
        public void demo1(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            CollectionBean collectionContext = (CollectionBean)applicationContext.getBean("collectionBean");
            System.out.println(collectionContext);
        }
    
    }

    测试类输出结果:

  • 相关阅读:
    哨兵模式(工作中使用)
    JVM调优-考虑方向
    Spring Cloud Gateway+Nacos出现服务乱串的问题记录
    golang笔记-cache组件应用: freecache/groupcache/golang-lru
    C++优化笔记: -O2/-O3/-ffast-math/SIMD
    linux笔记-查看L1/L2/L3 cache大小
    Dom4j 如何输出 Document 中的内容到文本
    是应该是用 Log 还是 Logger 来定义 Log
    IntelliJ IDEA 如何针对Java 代码快速打印 println
    如何用 Java 判断一个给定的数是不是素数
  • 原文地址:https://www.cnblogs.com/cuijiade/p/9573663.html
Copyright © 2011-2022 走看看