Spring6梳理10—— 依赖注入之注入数组类型属性

以上笔记来源:

尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc

目录

[10 依赖注入之注入数组类型属性](#10 依赖注入之注入数组类型属性)

[10.1 创建Emp实体类,Dept实体类](#10.1 创建Emp实体类,Dept实体类)

[10.2 创建bean-diarray配置文件](#10.2 创建bean-diarray配置文件)

[10.3 创建测试类](#10.3 创建测试类)


10 依赖注入之注入数组类型属性

10.1 创建Emp实体类,Dept实体类

并且在Emp类中添加了一个字符数组private String\[\] loves,设置了该数组的getter()和setter()方法,用于对象类型的注入,并且在work方法中添加了展示数组信息的方法

java 复制代码
package com.atguigu.spring6.iocxml.ditest;

import java.util.Arrays;

//员工类
public class Emp {
    private Dept dept;
    private String ename;
    private Integer age;
    private String[] loves;

    public String[] getLoves() {
        return loves;
    }

    public void setLoves(String[] loves) {
        this.loves = loves;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void work() {
        System.out.println(ename + "工作到了" + age);
        dept.info();
        System.out.println(Arrays.toString(loves));
    }
}

10.2 创建bean-diarray配置文件

在其中添加了property标签,array标签以及value标签

XML 复制代码
<?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="dept" class="com.atguigu.spring6.iocxml.ditest.Dept">
        <property name="dname" value="安保部"></property>
    </bean>


    <bean id="emp" class="com.atguigu.spring6.iocxml.ditest.Emp">
        <property name="ename" value="haozihua"></property>
        <property name="age" value="30"></property>
    <!--对象类型的属性-->
        <property name="dept" ref="dept"></property>
        <property name="loves">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>敲代码</value>
            </array>
        </property>
    </bean>

</beans>

10.3 创建测试类

java 复制代码
package com.atguigu.spring6.iocxml.ditest;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestEmpTest {

    //数组类型数据注入
    @Test
    public void testUser4() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-diarray.xml");
        Emp emp = context.getBean("emp", Emp.class);
        System.out.println("3 根据ID和class获取bean" + emp);
        emp.work();
    }
}
相关推荐
Jelena1577958579215 小时前
电商运营分析数据比价接口实战:多平台价格监控与智能决策系统
java·大数据·数据库
神明不懂浪漫16 小时前
【第五章】Java中的继承与多态
java·开发语言
AI多Agent协作实战派18 小时前
AI多Agent协作系统实战(十七):凌晨4点,我的AI系统在“假装工作“——3个bug同时爆炸的5小时
java·前端·bug
gaolei_eit18 小时前
Java+Ai+vue
java·spring·maven
qq_25183645718 小时前
基于java Web 动漫视频网站毕业论文
java·开发语言·前端
LayZhangStrive18 小时前
JUC相关的函数、注解、变量杂记
java·面试·多线程·juc
玉&心18 小时前
关于配置mcp服务端sse-message-endpoint和sse-endpoint的注意点
spring·springai·mcp
未秃头的程序猿18 小时前
给公司做了个AI客服Agent,用的Spring AI 1.0,3天上线领导拍板了
java·后端·ai编程
曹牧18 小时前
Eclipse 批量文本替换
java·ide·eclipse
程序员清风18 小时前
OpenAI官方发布最新提示词技巧!
java·后端·面试