【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());
    }
}
相关推荐
小明bishe186 小时前
计算机毕业设计之基于JAVA的植物科普网站
java·spring boot·spring·架构·课程设计
咖啡八杯8 小时前
GoF设计模式——模板方法模式
java·后端·spring·设计模式
huangjiazhi_18 小时前
纯C++实现ini文件操作
java·后端·spring
海天鹰18 小时前
WIN10修改窗口标题栏失去焦点背景色
windows
llilay19 小时前
企业级FastAPI后端模板搭建(六)加密用户密码
windows·fastapi
梦帮科技20 小时前
GRAVIS v5.5:向硬核桌面端进化与云端多节点容灾部署实践
windows·架构·rust·自动化·区块链·智能合约·数字货币
卓怡学长20 小时前
w272基于springboot便民医疗服务小程序
java·spring boot·spring·小程序·intellij-idea
霸道流氓气质20 小时前
Harness Engineering 从理论到实战:基于 Spring AI Alibaba 的完整实现指南
数据库·人工智能·spring·harness
tianyazhichiC21 小时前
【VLC player 下载地址】
windows
小呀小叮当~1 天前
OpenAI Codex 桌面版(Windows / macOS)体验分享:AI 编程效率再次提升
人工智能·windows·macos