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

news/2024/9/23 3:01:46/

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

目录

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

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

10.2  创建bean-diarray配置文件

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 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 {//数组类型数据注入@Testpublic 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();}
}


http://www.ppmy.cn/news/1529122.html

相关文章

Nginx泛域名 解析的匹配前缀绑定或转发到子目录

网站的目录结构为&#xff1a; # tree /home/wwwroot/landui.com /home/wwwroot/landui.com ├── bbs │ └── index.html └── www └── index.html 2 directories, 2 files /home/wwwroot/landui.com为nginx的安装目录下默认的存放源代码的路径。 bbs为论坛…

电气自动化入门07:开关电源、三相异步电动机多地与顺序控制电路

视频链接&#xff1a;3.5 电工知识&#xff1a;三相交流异步电动机多地与顺序控制及开关电源选型_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1PJ41117PW?p9&vd_sourceb5775c3a4ea16a5306db9c7c1c1486b5 1.开关电源功能与选型说明&#xff1a; 2.三相异步电动机…

WPF 异步

在 WPF 中&#xff0c;异步编程非常重要&#xff0c;尤其是为了保持 UI 线程的响应性。由于 WPF 的 UI 操作必须在主线程上进行&#xff0c;耗时的任务&#xff08;如文件读写、网络请求等&#xff09;如果直接在 UI 线程上执行&#xff0c;会导致 UI 冻结&#xff0c;界面无法…

rockylinux9.4单master节点k8s1.28集群部署

kubernetes集群部署 常见的 k8s 部署方式包括&#xff1a;二进制包、kubeadm 工具、云服务提供商、或通过一些开源的工具搭建&#xff0c;例如&#xff1a;sealos、kuboard、Runcher、kubeSphere。 本文使用kubeadm的部署方式&#xff0c;部署k8s1.28版本 我本地安装资源规划…

除猫毛用粘毛器还是宠物空气净化器?希喂/米家/352/范罗士/有哈空气净化器对比

微博之夜&#xff0c;明星互送礼物环节&#xff0c;要求所有嘉宾准备一份礼物&#xff0c;再由其他明星随机抽取互换礼物。田曦薇送粘毛器可是引起了广泛的争议和批评。不说价格&#xff0c;粘毛器对咱养猫人来讲还真是刚需啊。我朋友家三只猫&#xff0c;出门不用说啥&#xf…

【教程】鸿蒙ARKTS 打造数据驾驶舱---前序

鸿蒙ARKTS 打造数据驾驶舱 ​ 前面2章我介绍了如何通过定义View绘制箭头以及圆形进度&#xff0c;初步了解了鸿蒙如何进行自定义View。接下来我将通过我最近在带的一个VUE的项目&#xff0c;简单实现了几个鸿蒙原生页面。帮助大家快速上手纯血鸿蒙开发. 本项目基于Api11Stage模…

Web APIs 2:事件监听

Web APIs 2&#xff08;事件监听&#xff09; 1.事件监听 语法&#xff1a; 元素对象.addEventListener(‘事件类型’&#xff0c;要执行的函数) 事件源&#xff1a;获取的dom元素事件类型&#xff1a;用什么方式触发&#xff0c;比如鼠标单击click、鼠标经过mouseover等事件调…

Mac 搭建仓颉语言开发环境(Cangjie SDK)

文章目录 仓颉编程语言通用版本SDK Beta试用报名仓颉语言文档注册 GitCode登录 GitCode 下载 Cangjie SDK配置环境变量VSCode 插件VSCode 创建项目 仓颉编程语言通用版本SDK Beta试用报名 https://wj.qq.com/s2/14870499/c76f/ 仓颉语言文档 https://developer.huawei.com/c…