【Spring】依赖注入(array , list ,map)

java 复制代码
public class Student{
    private String[]  books;
    private List<String> hobbies;
    private Map<String,String> card;

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    @Override
    public String toString() {
        return "Student{" +
                "books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                '}';
    }
}
java 复制代码
<?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="student"  class="org.example.Student">
        <!-- array -->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>

        <!-- array -->
        <property name="hobbies">
            <list>
                <value>篮球</value>
                <value>画画</value>
                <value>音乐</value>
            </list>
        </property>

        <!--map -->
        <property name="card">
            <map>
                <entry key="身份证" value="1111111" />
                <entry key="银行卡"   value="22222" />
            </map>
        </property>

    </bean>


</beans>
java 复制代码
public class Test4 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student=(Student) context.getBean("student");
        System.out.println(student.toString());
    }
}
相关推荐
折哥的程序人生 · 物流技术专研1 分钟前
Java 23 种设计模式:从踩坑到精通 | 番外:状态 vs 策略 —— if-else 消除的两条路,你走对了吗?
java·spring·状态模式·策略模式·java面试·java设计模式·从踩坑到精通
我是Superman丶13 分钟前
Windows系统FFmpeg官方下载+完整安装配置保姆级教程(2026最新版)
windows·ffmpeg
weixin_433417671 小时前
TortoiseGit推送到远端,如何配置
windows·git
折哥的程序人生 · 物流技术专研1 小时前
Java 23 种设计模式:从踩坑到精通 | 番外:工厂方法 vs 抽象工厂 —— 从单产品到产品族,架构如何演进?
java·spring·java面试·抽象工厂·工厂方法·java设计模式·从踩坑到精通
砍材农夫1 小时前
spring|spring event|Spring内置事件驱动编程模型
java·数据库·spring
2601_9623649711 小时前
Windows Hello VS 传统密码:身份认证安全层级全面对比分析
windows·安全·电脑
heimeiyingwang13 小时前
【架构实战】缓存一致性:Cache Aside与双写问题的破解
spring·缓存·架构
栈溢出了14 小时前
Redis 分片集群与哈希槽笔记
java·redis·笔记·spring
Imagine Miracle16 小时前
【WSL】让WSL2后台持久运行不自动关闭的解决方案
linux·windows·wsl
啦啦啦~~~22216 小时前
经典小游X,免费刺激,可离线使用!
windows·游戏·开源软件