【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());
    }
}
相关推荐
为爱停留17 分钟前
Spring AI实现RAG(检索增强生成)详解与实践
人工智能·深度学习·spring
静水楼台x1 小时前
Java之String系列--intern方法的作用及原理
java·spring
谷哥的小弟2 小时前
Spring Framework源码解析——GenericTypeResolver
java·spring·源码
diegoXie2 小时前
【Python】 中的 * 与 **:Packing 与 Unpacking
开发语言·windows·python
廋到被风吹走3 小时前
【Spring】事务管理深度解析|从原理到实战
java·spring
IT_Octopus6 小时前
java多线程环境下 安全地初始化缓存(避免缓存击穿),同时兼顾性能 的双重检查锁方案
java·spring·缓存
亮子AI7 小时前
【Tiptap】如何使用 ordered list?
数据结构·list·tiptap
武藤一雄8 小时前
C#:Linq大赏
windows·后端·microsoft·c#·.net·.netcore·linq
Java水解9 小时前
【Spring Boot 单元测试教程】从环境搭建到代码验证的完整实践
后端·spring
少许极端10 小时前
Redis入门指南:从零到分布式缓存-hash与list类型
redis·分布式·缓存·list·hash