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();
    }
}
相关推荐
哞哞不熬夜11 分钟前
JavaEE--初识网络
java·网络·java-ee
等等54324 分钟前
Java EE初阶——wait 和 notify
java·开发语言
沛沛老爹44 分钟前
软件架构风格系列(2):面向对象架构
spring·软件架构风格·面向对象架构·架构入门
API小爬虫44 分钟前
淘宝按图搜索商品(拍立淘)Java 爬虫实战指南
java·爬虫·图搜索算法
lyrhhhhhhhh1 小时前
Spring 框架 JDBC 模板技术详解
java·数据库·spring
亚林瓜子2 小时前
AWS Elastic Beanstalk控制台部署Spring极简工程
java·spring·云计算·aws·eb
2401_cf2 小时前
如何创建maven项目
java·maven·intellij-idea
亚林瓜子3 小时前
Spring集成Redis中禁用主机名DNS检测
redis·spring·ssh
C4程序员3 小时前
Java百度身份证识别接口实现【配置即用】
java·开发语言
炒空心菜菜3 小时前
MapReduce 实现 WordCount
java·开发语言·ide·后端·spark·eclipse·mapreduce