【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());
    }
}
相关推荐
开发小程序的之朴5 小时前
安企CMS的安装-PHPStudy(小皮面板)部署
windows·mysql·nginx
love530love5 小时前
【硬核排障 & 猴子补丁 & 幽灵节点】SageAttention GQA 崩溃与 Flash Attention 兼容性修复全记录
ide·人工智能·windows·comfyui·sageattention·windows cuda
江屿风6 小时前
【C++笔记】List流食般投喂
开发语言·c++·笔记·list
John Song15 小时前
ansible-playbook常用的检查命令
windows·ansible
视觉AI16 小时前
VS Code Remote-SSH 连接Jetson踩坑完整解决记录(网段不通+主机密钥变更双重故障)
运维·网络·人工智能·windows·ssh·边缘计算
霸道流氓气质1 天前
Spring AI Alibaba Copilot 项目使用指南
人工智能·spring·copilot
观远数据1 天前
从离线开发到实时同步:DataFlow如何支撑企业级数据治理闭环
java·windows·microsoft·excel
绀目澄清1 天前
私服网游云加速程序劫持流量排查与修复总结
windows·安全
FL16238631291 天前
Windows手动下载安装配置uv 0.11.29配置国内源含安装包和安装教程
windows·uv
Jelena157795857921 天前
淘宝商品详情API接口开发指南:应用市场选品比价实战
windows·microsoft