【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());
    }
}
相关推荐
一路向北North25 分钟前
Spring Security OAuth2.0(22):分布式系统授权-搭建网关
java·后端·spring
尚早立志1 小时前
六)Spring Boot 源码研读之prepareContext 准备上下文
java·spring boot·后端·spring
XUHUOJUN15 小时前
Windows 2025架构深度分析之Stretch Cluster 延迟工程现实
windows·microsoft·架构·azure local
love530love15 小时前
将 ChatCut MCP 插件从 Codex 桌面应用移植到 WorkBuddy —— 完整适配实录
ide·人工智能·windows·视频剪辑·ai agent
works cart16 小时前
windows常用命令
windows
浩浩测试一下18 小时前
windows 加壳工具代码实现02 - 加壳界面实现MFC
windows·mfc·免杀·加壳器·pe应用
谙忆20 小时前
图片CDN与边缘优化实战:缓存策略、图片处理型CDN与动态裁剪
java·spring·缓存
崖边看雾21 小时前
记录Python学习——第一章环境安装下载(Windows)
windows·python·学习·pycharm
web行路人1 天前
Spring Boot 第四周:集成 Spring Security 与 JWT 鉴权实践
spring boot·后端·spring
Xiaouuuuua1 天前
Superpowers:GPT-5.6 时代下的流程毒瘤
java·gpt·spring