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}

相关推荐
heartbeat..4 分钟前
Redis 常用命令全解析:基础、进阶与场景化实战
java·数据库·redis·缓存
让我上个超影吧5 分钟前
天机学堂——多级缓存
java·spring boot·spring cloud
Yvonne爱编码19 分钟前
Java 接口学习核心难点深度解析
java·开发语言·python
带刺的坐椅20 分钟前
Solon AI Remote Skills:开启分布式技能的“感知”时代
java·llm·solon·mcp·skills
这周也會开心33 分钟前
SSM 配置 index 页面的实现方式
java·tomcat·springmvc
黎雁·泠崖38 分钟前
Java继承入门:概念+特点+核心继承规则
java·开发语言
sheji34161 小时前
【开题答辩全过程】以 小区物业管理APP为例,包含答辩的问题和答案
java
星辰徐哥1 小时前
Java程序的编译与运行机制
java·开发语言·编译·运行机制
老毛肚1 小时前
Spring 6.0基于JDB手写定制自己的ROM框架
java·数据库·spring
Sylvia-girl1 小时前
线程安全问题
java·开发语言·安全