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}

相关推荐
程序猿秃头之路10 分钟前
DDD 系列:DTO、VO、DO、Entity 怎么区分
java·ddd·领域驱动设计
自由随风飘1 小时前
JAVA中的面向对象-3
java·开发语言
桐薇全肯定1 小时前
MySQL学生成绩管理系统实战操作
java·数据库·sql
Nuanyt2 小时前
SSM 学习记录 第二部分 Spring整合Mybatis&Junit AOP核心概念 Spring事务管理 SpringMVC 请求与响应 Rest风格
java·spring·junit·mybatis·restful
醉城夜风~2 小时前
C++模板详解:从基础到高级全面解析
java·开发语言·c++
米码收割机2 小时前
【SSM】Spring MVC_MyBatis SSM商城系统(源码+论文)【独一无二】
spring·mvc·mybatis
程序员天天困3 小时前
Arthas Profiler 火焰图实战:CPU 热点在哪一目了然
java·jvm·后端
我命由我123453 小时前
Android 在构建过程中,发现有两个依赖库都包含了相同路径的资源文件
android·java·开发语言·java-ee·kotlin·android-studio·android runtime
用户3126874877203 小时前
@Autowired 注入的是代理对象?Spring AOP 代理选择原理一次讲透
java·spring