• Spring集合注入


    一、环境准备

    • 创建一个Maven项目
    • pom.xml添加Spring依赖
    • resources下添加spring的配置文件applicationContext.xml
    • 项目中添加BookDao、BookDaoImpl类
    public interface BookDao {
        public void save();
    }
    
    • 1
    • 2
    • 3
    public class BookDaoImpl implements BookDao {
       private int[] array;
       private List list;
       private Set set;
       private Map map;
       private Properties properties;
    
       public void setArray(int[] array) {
          this.array = array;
       }
    
       public void setList(List list) {
          this.list = list;
       }
    
       public void setSet(Set set) {
          this.set = set;
       }
    
       public void setMap(Map map) {
          this.map = map;
       }
    
       public void setProperties(Properties properties) {
          this.properties = properties;
       }
       public void save(){
          System.out.println("book dao save...");
          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
    • resources下提供spring配置文件
    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 编写AppForDICollection运行类,加载Spring的IOC容器,并从中获取对应bean对象
    public class AppForDICollection {
        public static void main(String[] args) {
            ApplicationContext c= new ClassPathXmlApplicationContext("applicationContext.xml");
            BookDao bookdao= (BookDao) c.getBean("bookDao");
            bookdao.save();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二、注入数组类型数据

    
                
                    100
                    200
                    300
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三、注入List类型数据

    
                
                    java
                    spring
                    mysql
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    四、注入Map类型数据

     
                
                    
                    
                    
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五、注入Set类型数据

    
                
                    springboot
                    vue
                    html
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    六、注入Properties类型数据

     
                
                    china
                    guangdong
                    guangzhou
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置完成后运行AppForCollection程序,运行结果为:
    在这里插入图片描述

    • property标签表示setter方式注入,构造方式注入constructor-arg标签内部也可以写、、、、标签。
    • List的底层也是通过数组实现的,所以和标签是可以混用。
    • 集合中要添加引用类型,只需要把标签改成标签,这种方式用的比较少。
  • 相关阅读:
    Linux0.12内核源码解读(2)-Bootsect.S
    四、【常用的几种抠图方式三】
    服装行业的CRM品牌供应商如何选型?
    SQL:With recursive 递归用法
    5G消息.
    [JVM] 浅析JVM的垃圾回收机制的相关概念
    ios系统元素悬浮滑动错乱,和ios页面无故刷新问题
    深入浅出的算法设计与分析技巧解读(软件设计师笔记)
    JVM学习(三)--运行时数据区
    【栈的应用】数据结构之栈的几种常见题目(数制转换、括号匹配、汉诺塔、迷宫求解)
  • 原文地址:https://blog.csdn.net/m0_46550052/article/details/128086096