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}

相关推荐
h***047736 分钟前
SpringBoot(7)-Swagger
java·spring boot·后端
v***91302 小时前
Spring boot创建时常用的依赖
java·spring boot·后端
代码or搬砖5 小时前
MyBatisPlus讲解(二)
java·mybatis
lcu1115 小时前
Java 学习42:抽象
java
Mr.朱鹏5 小时前
RocketMQ安装与部署指南
java·数据库·spring·oracle·maven·rocketmq·seata
雨中飘荡的记忆5 小时前
Spring表达式详解:SpEL从入门到实战
java·spring
Coder-coco5 小时前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文
5***26225 小时前
Spring Boot问题总结
java·spring boot·后端
xkroy6 小时前
Spring Boot日志
java·spring boot·后端
n***F8756 小时前
【Spring Boot】SpringBoot自动装配-Import
java·spring boot·后端