• 集合注入实例(八)


    文章目录

    一、案例

    1.BookDao接口

    package com.jkj.dao;
    public interface BookDao {
        void save();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.BookDaoImpl实现类

    package com.jkj.dao.impl;
    
    import com.jkj.dao.BookDao;
    
    import java.util.*;
    
    public class BookDaoImpl implements BookDao {
        private int [] array;
        private List<String> list;
        private Set<String> set;
        private Map<String,String> map;
        private Properties properties;
    
        public void setArray(int[] array) {
            this.array = array;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        public void setSet(Set<String> set) {
            this.set = set;
        }
    
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public void save() {
            System.out.println("BookDao...");
            System.out.println("遍历数组:"+ Arrays.toString(array));
            System.out.println("遍历List:"+list);
            System.out.println("遍历Set:"+set);
            System.out.println("遍历Map:"+map);
            System.out.println("遍历Properties:"+properties);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    3.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="bookDao" class="com.jkj.dao.impl.BookDaoImpl">
            <property name="array">
                <array>
                    <value>12</value>
                    <value>14</value>
                    <value>16</value>
                </array>
            </property>
            <property name="list">
                <list>
                    <value>小马哥</value>
                    <value>Tom</value>
                    <value>小飞侠</value>
                </list>
            </property>
            <property name="set">
                <set>
                    <value>Cat</value>
                    <value>Cat</value>
                    <value>Tom</value>
                    <value>Jack</value>
                </set>
            </property>
            <property name="map">
                <map>
                   <entry key="1" value="34"></entry>
                    <entry key="2" value="小飞侠"></entry>
                    <entry key="3" value="Rack"></entry>
                </map>
            </property>
            <property name="properties">
                <props>
                   <prop key="1">科比</prop>
                    <prop key="2">詹姆斯</prop>
                    <prop key="3">哈登</prop>
                </props>
            </property>
    
    
        </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    4.Test测试类

    import com.jkj.dao.BookDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            BookDao bookDao = (BookDao) context.getBean("bookDao");
            bookDao.save();
        }
    }
    /*
    BookDao...
    遍历数组:[12, 14, 16]
    遍历List:[小马哥, Tom, 小飞侠]
    遍历Set:[Cat, Tom, Jack]
    遍历Map:{1=34, 2=小飞侠, 3=Rack}
    遍历Properties:{3=哈登, 2=詹姆斯, 1=科比}
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    C++设计模式之——桥接模式详解和代码实例
    《Redis 设计与实现》读书概要
    关系型数据库语言基础整理
    C++多线程编程(第三章 案例3:把案例1改装成案例2的条件变量多线程方式)
    【单片机】数码管的使用
    交互与前端3 前端需求简单梳理
    Elasticsearch7.8.1集群安装手册
    (十六)模板与泛型编程
    达梦数据库在不修改SQL的情况下为SQL指定HINT
    数组 vector
  • 原文地址:https://blog.csdn.net/qq_43514330/article/details/125424481