Spring DI 数据类型—— set 方法注入

2024-08-22 15:12

本文主要是介绍Spring DI 数据类型—— set 方法注入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先新建项目,可参考 初识IDEA、模拟三层--控制层、业务层和数据访问层

一、spring 环境搭建

(一)pom.xml 导相关坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
<!--    导坐标,不导入该坐标会影响xml文件创建
导完坐标,一定去该页面右边点开 Maven看看是否下载完成,
若不报错,即下载成功--><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.28</version></dependency></dependencies><groupId>org.example</groupId><artifactId>Spring_ioc_04</artifactId><version>1.0-SNAPSHOT</version>
</project>

(二)搭建配置文件

依次点击:src ==> main ==> resource 右击  ==> New ==> 找 XML Configuration File ==> 找到 Spring Config 并点击,起名(自己随便起名字),为了好区分,我起名叫 applicationContext

(三)建包建类,写方法

        建包建类,可参考模拟三层--控制层、业务层和数据访问层,我们建立 com.apesource路径(这个路径纯纯属于个人习惯,不影响)下的pojo 包。pojo 包写我们的普通的类,我们写个学生类 Student 吧,类中写好成员变量;text 包就写测试类,具体代码如下:

//Student 学生类
package com.apesource.pojo;public class Student {private int sid;private String sname;private int sage;
}
//测试类
package com.apesource.test;public class Test01 {
}

Ok!一切准备就绪,开始演示 DI 数据类型

二、演示 DI 数据类型

2.1 set 注入基本类型与 String

我们要用 setter 方法注入 ,首先得有 set 方法,先去学生类 alt键+ins键 快速生成 set 方法(更多的快捷键使用可以去看看 初识 IDEA)

package com.apesource.pojo;public class Student {private int sid;private String sname;private int sage;public void setSid(int sid) {this.sid = sid;}public void setSname(String sname) {this.sname = sname;}public void setSage(int sage) {this.sage = sage;}
}

 然后去我们的 applicationContext.xml 文件做相关配置【这块想了解更清楚可以点击链接; Spring DI 简单演示三层架构——Setter 注入】。

<?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">
<!--    注入 Student 类--><!--property的name指要通过 setter 给注入的属性名--><!--property的value指要通过 setter 给注入的数据,写具体值--><!--property的ref可以直接引用标签id,Spring DI 简单演示三层架构——set注入有演示--><bean class="com.apesource.pojo.Student" id="student"><property name="sage" value="12"></property><property name="sid" value="1"></property><property name="sname" value="唐三"></property></bean>
</beans>

测试【这块想了解更清楚可点击链接Spring DI 简单演示三层架构——Setter 注入】。

package com.apesource.test;import com.apesource.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {public static void main(String[] args) {//1.加载 Spring 核心配置文件,获取 Spring 容器对象ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//2.获取对象Student student = (Student) applicationContext.getBean("student");System.out.println(student);//返回一个学生的地址}}

测试结果(有地址,测试成功):


如果想看到具体值,我们可以去 Student 类写 get 方法,然后这边调用 get 方法,具体步骤如下:

package com.apesource.pojo;public class Student {private int sid;private String sname;private int sage;public void setSid(int sid) {this.sid = sid;}public void setSname(String sname) {this.sname = sname;}public void setSage(int sage) {this.sage = sage;}
/************************get方法是为了测试调用可以看到具体值*************************/public int getSid() {return sid;}public String getSname() {return sname;}public int getSage() {return sage;}
}
package com.apesource.test;import com.apesource.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {public static void main(String[] args) {//1.加载 Spring 核心配置文件,获取 Spring 容器对象ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//2.获取对象Student student = (Student) applicationContext.getBean("student");System.out.println("对象地址:"+student);//返回一个学生的地址/******************************为了更方便的查看学生信息,我们调用get方法调用**********************************/System.out.println("学生的年龄:"+student.getSage());System.out.println("学生的名字:"+student.getSname());System.out.println("学生的id号:"+student.getSid());}}

测试结果(这个学生的信息就是我们在 application.xml 中创建的信息,测试成功):


如果想看到具体值,我们也可以去 Student 类写 toString 方法,然后测试就可以看到具体值,具体步骤如下:

package com.apesource.pojo;public class Student {private int sid;private String sname;private int sage;public void setSid(int sid) {this.sid = sid;}public void setSname(String sname) {this.sname = sname;}public void setSage(int sage) {this.sage = sage;}
/*********************to String方法为了测试有显示结果****************************/@Overridepublic String toString() {return "Student{" +"sid=" + sid +", sname='" + sname + '\'' +", sage=" + sage +'}';}
}
package com.apesource.test;import com.apesource.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {public static void main(String[] args) {//1.加载 Spring 核心配置文件,获取 Spring 容器对象ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//2.获取对象Student student = (Student) applicationContext.getBean("student");System.out.println(student);//返回一个学生信息}

测试结果:

2.2 set 注入复杂类型【 List 、Map 、Set、array数组、properties 】

         首先,我们建一个新的包新的类,可参考模拟三层--控制层、业务层和数据访问层,我们在 com.apesource路径下的pojo 包里再写个 Collection 集合类吧,类中写好成员变量,我们演示复杂类型,就定义变量为 List 、Map 、Set、array数组、properties  这些复杂的类型:

package com.apesource;import java.util.*;public class Collection {private List list;private Map map;private Set set;private String[] array;private Properties properties;/****************因为使用set注入,要写set方法****************/public void setList(List list) {this.list = list;}public void setMap(Map map) {this.map = map;}public void setSet(Set set) {this.set = set;}public void setArray(String[] array) {this.array = array;}public void setProperties(Properties properties) {this.properties = properties;}/****************为了观察到测试结果的具体值,写 toString方法****************/@Overridepublic String toString() {return "collectionT{" +"list=" + list +", map=" + map +", set=" + set +", array=" + Arrays.toString(array) +", properties=" + properties +'}';}
}

然后去我们的 applicationContext.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"><!--    注入 Collection 类--><bean class="com.apesource.pojo.Collection" id="collection"><!--List集合--><property name="list"><list><value>油条</value><value>玉米</value><value>包子</value><value>红薯</value><value>胡辣汤</value></list></property><!--Map集合——键值对--><property name="map"><map><entry key="红" value="蓝"></entry><entry key="白" value="黑"></entry><entry key="光明" value="黑暗"></entry><entry key="正义" value="邪恶"></entry><entry key="快乐" value="悲伤"></entry></map></property><!--Set集合--><property name="set"><set><value>唐三</value><value>小舞</value><value>戴沐白</value><value>朱竹清</value><value>宁荣荣</value><value>奥斯卡</value><value>胖子</value></set></property><!--数组--><property name="array"><array><value>喜羊羊</value><value>美羊羊</value><value>懒羊羊</value><value>沸羊羊</value><value>暖羊羊</value><value>慢羊羊村长</value></array></property><!--set诸如还支持properties,下面演示--><property name="properties"><props>
<!--                <prop key="键">值</prop>--><prop key="中文">英文</prop><prop key="名字">name</prop><prop key="age">年龄</prop><prop key="语文">数学</prop><prop key="学校">学习</prop></props></property></bean>
</beans>

测试:

package com.apesource.test;import com.apesource.pojo.Collection;
import com.apesource.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class Test01 {public static void main(String[] args) {//1.加载 Spring 核心配置文件,获取 Spring 容器对象ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//2.获取对象Collection collection = (Collection)applicationContext.getBean("collection");System.out.println(collection);}}

测试结果(由于所有结果在一行,截图看不完,我将该主要结果复制在下方):

完整结果:

collectionT{list=[油条, 玉米, 包子, 红薯, 胡辣汤], map={红=蓝, 白=黑, 光明=黑暗, 正义=邪恶, 快乐=悲伤}, set=[唐三, 小舞, 戴沐白, 朱竹清, 宁荣荣, 奥斯卡, 胖子], array=[喜羊羊, 美羊羊, 懒羊羊, 沸羊羊, 暖羊羊, 慢羊羊村长], properties={语文=数学, age=年龄, 名字=name, 学校=学习, 中文=英文}}

三、一个类可以被注入多次,但id需唯一

拿简单的学生类做演示吧

学生类不做任何改变,在我们的 applicationContext.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">
<!--    注入 Student 类-->
<!--    property的name指要通过 setter 给注入的属性名-->
<!--    property的value指要通过 setter 给注入的数据,写具体值-->
<!--    property的ref可以直接引用标签id--><bean class="com.apesource.pojo.Student" id="student"><property name="sage" value="12"></property><property name="sid" value="1"></property><property name="sname" value="唐三"></property></bean><bean class="com.apesource.pojo.Student" id="student1"><property name="sage" value="12"></property><property name="sid" value="2"></property><property name="sname" value="小舞"></property></bean>
</beans>

测试:

package com.apesource.test;import com.apesource.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {public static void main(String[] args) {//1.加载 Spring 核心配置文件,获取 Spring 容器对象ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//2.获取对象Student student = (Student) applicationContext.getBean("student");System.out.println(student);//返回一个学生信息Student student1 = (Student) applicationContext.getBean("student1");System.out.println(student1);//返回一个学生信息}
}

测试结果(两个学生都打印出来了,测试成功):

四、总结

DI 数据类型 set 方法可注入三种:

        基本类型与String

        复杂类型,list,set,array,map,properties

        javaBean对象,具体实例可点击链接Spring DI 简单演示三层架构——Setter 注入】

一个类可以被注入多次,但id需唯一

这篇关于Spring DI 数据类型—— set 方法注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1096631

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三