【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());
    }
}
相关推荐
jessecyj4 分钟前
maven导入spring框架
数据库·spring·maven
闭关苦炼内功8 分钟前
Win10 安装 MySQL5.7.36 数据库记录
数据库·windows·mysql
小江的记录本18 分钟前
【Redis】Redis常用命令速查表(完整版)
java·前端·数据库·redis·后端·spring·缓存
卓怡学长18 分钟前
m281基于SSM框架的电脑测评系统
java·数据库·spring·tomcat·maven·intellij-idea
Densen201425 分钟前
企业H5站点升级PWA (二)
java·后端·spring
marsh020630 分钟前
16 openclaw与数据库集成:ORM使用与性能优化
数据库·spring·ai·性能优化·编程·技术
进击的野人1 小时前
从AI“说人话”到“说结构话”:Spring AI结构化输出实战解析
人工智能·spring·ai编程
计算机学姐1 小时前
基于SpringBoot的校园二手交易系统
java·vue.js·spring boot·后端·spring·tomcat·intellij-idea
斯密码赛我是美女1 小时前
周报(欢乐赛+信息搜集ctfshow+Trae-mcp)
网络·windows
米饭不加菜1 小时前
Windows 键盘快捷方式总览
windows·计算机外设