spring 集合注入格式

数组

List

Set

Map

properties

案例

java 复制代码
package org.example.dao.impl;

import org.example.dao.BookDao;

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

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(this.array);
        System.out.println(this.list);
        System.out.println(this.set);
        System.out.println(this.map);
        System.out.println(this.properties);
    }
}
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="org.example.dao.impl.BookDaoImpl">
        <property name="array">
            <array>
                <value>10</value>
                <value>20</value>
                <value>30</value>
            </array>
        </property>
        <property name="list">
           <list>
               <value>aaa</value>
               <value>bbb</value>
               <value>ccc</value>
           </list>
        </property>
        <property name="set">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
                <value>ccc</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="name" value="alex"/>
                <entry key="city" value="上海"/>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="name">alex</prop>
                <prop key="city">上海</prop>
            </props>
        </property>
    </bean>

</beans>

输出

I@22635ba0 \[aaa, bbb, ccc

aaa, bbb, ccc

{name=alex, city=上海}

{city=上海, name=alex}

相关推荐
沉鱼.4427 分钟前
第十二届题目
java·前端·算法
赫瑞1 小时前
数据结构中的排列组合 —— Java实现
java·开发语言·数据结构
周末也要写八哥2 小时前
多进程和多线程的特点和区别
java·开发语言·jvm
惜茶3 小时前
vue+SpringBoot(前后端交互)
java·vue.js·spring boot
杰克尼4 小时前
springCloud_day07(MQ高级)
java·spring·spring cloud
NHuan^_^5 小时前
SpringBoot3 整合 SpringAI 实现ai助手(记忆)
java·人工智能·spring boot
Mr_Xuhhh6 小时前
从ArrayList到LinkedList:理解链表,掌握Java集合的另一种选择
java·数据结构·链表
错把套路当深情6 小时前
Java 全方向开发技术栈指南
java·开发语言
han_hanker6 小时前
springboot 一个请求的顺序解释
java·spring boot·后端
杰克尼6 小时前
SpringCloud_day05
后端·spring·spring cloud