• 【Spring笔记03】Spring依赖注入各种数据类型


    这篇文章,详细介绍一下Spring框架中如何注入各种数据类型,包含:注入基本数据类型、数组、集合、Map映射、Property属性、注入空字符串、注入null值、注入特殊字符等内容,以及如何使用命名空间进行依赖注入。

    目录

    一、注入各种数据类型

    1.1、注入基本类型

    1.2、注入Bean对象

    (1)外部注入Bean方式

    (2)内部注入Bean方式

    1.3、注入数组

    1.4、注入List集合

    1.5、注入Set集合

    1.6、注入Map

    1.7、注入Property属性

    1.8、注入null值和空值

    1.9、注入特殊字符

    二、命名空间

    2.1、p命名空间

    2.2、c命名空间

    2.3、util命名空间


    一、注入各种数据类型

    1.1、注入基本类型

    注入基本数据类型,既可以采用setter方法注入,也可以采用构造方法注入,两种方式都有不同的限制条件:

    • setter方法注入:必须确保注入的属性存在setXXX()方法,否则注入失败。
    • 构造方法注入:必须确保Bean对象具有相应的有参构造方法,否则注入失败。

    这里我以setter方法注入为案例:


    以上,就是setter方法注入的用法,只要在Spring的XML配置文件里面采用【】标签,指定属性名称和属性值就可以啦。

    1.2、注入Bean对象


    在实际开发过程中,我们一个类里面会用到另外一个类的对象,这个时候就是注入引用对象类型,也就是我们说的Bean对象。注入Bean对象,首先需要确保Bean对象已经被实例化了,然后另外一个Bean里面才能够注入。

    注入Bean分为两种:

    • 外部注入Bean方式
    • 内部注入Bean方式

    (1)外部注入Bean方式

    外部注入Bean方式,是指:A类中使用了B类的对象,那么首先需要将B类对象进行实例化,然后A类直接引用B类实例化之后的对象。

    这里举个栗子,员工和部门的关系,我们就可以两个类来表示,一个是【Emp】员工类,另外一个是【Dept】部门类,案例代码如下所示:

    创建【Dept】类:

    1. public class Dept {
    2.     private Integer deptId;
    3.     private String deptName;
    4.     public Integer getDeptId() {
    5.         return deptId;
    6.     }
    7.     public void setDeptId(Integer deptId) {
    8.         this.deptId = deptId;
    9.     }
    10.     public String getDeptName() {
    11.         return deptName;
    12.     }
    13.     public void setDeptName(String deptName) {
    14.         this.deptName = deptName;
    15.     }
    16. }

    创建【Emp】类:

    1. public class Emp {
    2.     private Integer empId;
    3.     private String empName;
    4.     private Dept dept; // 部门类的对象
    5.     public Integer getEmpId() {
    6.         return empId;
    7.     }
    8.     public void setEmpId(Integer empId) {
    9.         this.empId = empId;
    10.     }
    11.     public String getEmpName() {
    12.         return empName;
    13.     }
    14.     public void setEmpName(String empName) {
    15.         this.empName = empName;
    16.     }
    17.     public Dept getDept() {
    18.         return dept;
    19.     }
    20.     public void setDept(Dept dept) {
    21.         this.dept = dept;
    22.     }
    23. }

    添加XML配置


    我们把这种直接引用外部Bean的方式,叫做注入外部Bean。

    可以发现,我们这里使用的【】标签是通过【ref】属性进行注入Bean的,而不是【value】属性,相信你应该知道这两个的区别了吧。

    • value属性:用于注入基本数据类型、字符串之类的。
    • ref属性:用于引用另外一个Bean的实例对象。

    (2)内部注入Bean方式

    外部注入Bean方式是引用外部的一个Bean实例,那内部注入Bean就是不引用外部的Bean呗,而是直接在当前的Bean里面进行实例化。

    具体XML配置如下所示:

    1. <bean id="emp2" class="com.spring.demo.pojo.Emp">
    2.    
    3.     <property name="empId" value="1001"/>
    4.     <property name="empName" value="张三"/>
    5.    
    6.     <property name="dept">
    7.         <bean class="com.spring.demo.pojo.Dept">
    8.            
    9.             <property name="deptId" value="2001"/>
    10.             <property name="deptName" value="研发部"/>
    11.         bean>
    12.     property>
    13. bean>

    是不是很简单,内部注入bean其实就是把另外一个bean的配置移到【】标签之间。

    1.3、注入数组

    如果类中的某个属性是数组,那就不能和之前一样编写了,因为之前的都是注入单独一个的值,而数组可以有多个值,那么如何注入数组呢???

    注入数组,可以借助【】标签,看个案例就知道啦。


    采用【】标签告诉Spring,当前这个属性是数组类型的,通过【】标签,告诉Spring数组里面的数值是哪些。

    1.4、注入List集合

    注入集合和注入数组类似的,只不过注入List集合是采用【】标签进行标识。

    1. <bean id="collectionType1" class="com.spring.demo.pojo.CollectionType">
    2.    
    3.     <property name="listData">
    4.         <list>
    5.             <value>张三value>
    6.             <value>李四value>
    7.             <value>王五value>
    8.         list>
    9.     property>
    10. bean>

    1.5、注入Set集合

    注入Set集合是采用【】标签进行标识。

    1. <bean id="collectionType2" class="com.spring.demo.pojo.CollectionType">
    2.    
    3.     <property name="setData">
    4.         <set>
    5.             <value>张三value>
    6.             <value>李四value>
    7.             <value>王五value>
    8.         set>
    9.     property>
    10. bean>

    1.6、注入Map

    注入map比较特殊,因为map和List、Set集合不同,map是具有key-value形式的数据,所以注入map集合,需要采用【】标签定义,然后通过【】标签进行取值。

    】标签具有如下属性:

    • key属性:指定map集合的key值。
    • value属性:指定map集合的value值。
    • ref属性:指定map集合的value值(引用另外一个bean对象)。
    • key-ref属性:key值引用另外一个Bean对象。
    • value-ref属性:value值引用另外一个Bean对象。

    具体案例代码如下所示:

    1. <bean id="collectionType3" class="com.spring.demo.pojo.CollectionType">
    2.    
    3.     <property name="map">
    4.         <map>
    5.             <entry key="1" value="Tom"/>
    6.             <entry key="2" value="Jerry"/>
    7.             <entry key="3" value="Jack"/>
    8.         map>
    9.     property>
    10. bean>

    1.7、注入Property属性

    Property是HashMap的一个子类,它也是满足key-value形式的数据集合,通过【】标签定义,然后采用【】标签进行key和value的赋值。另外,还可以读取外部的【property】文件的内容进行注入。

    注入Property属性案例:

    1. <bean id="collectionType4" class="com.spring.demo.pojo.CollectionType">
    2.    
    3.     <property name="properties">
    4.         <props>
    5.             <prop key="1">Tomprop>
    6.             <prop key="2">Jerryprop>
    7.             <prop key="3">Jackprop>
    8.         props>
    9.     property>
    10. bean>

    如何定义Property文件???

    首先需要创建一个以【.properties】为后缀的文件。

    然后在文件中,通过【key=value】的格式,编写对应的数据内容。

    注意:一行只能编写一个【key=value】。

    从Property文件读取属性案例:

    • 在【src/main/resources】目录下,创建一个【jdbc.properties】文件
    1. driver=com.jdbc.mysql.Driver
    2. url=jdbc:mysql://localhost:3306/test
    3. username=root
    4. password=root
    • 创建【JdbcProperties】类
    1. public class JdbcProperties {
    2.     private String driver;
    3.     private String url;
    4.     private String username;
    5.     private String password;
    6.     // setter and getter
    7. }
    • XML配置文件中添加context命名空间
    • 注入属性
    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:context="http://www.springframework.org/schema/context"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.        http://www.springframework.org/schema/beans/spring-beans.xsd
    7.        http://www.springframework.org/schema/context
    8.        http://www.springframework.org/schema/context/spring-context.xsd">
    9.    
    10.     <bean id="collectionType4" class="com.spring.demo.pojo.CollectionType">
    11.        
    12.         <property name="properties">
    13.             <props>
    14.                 <prop key="1">Tomprop>
    15.                 <prop key="2">Jerryprop>
    16.                 <prop key="3">Jackprop>
    17.             props>
    18.         property>
    19.     bean>
    20.    
    21.     <context:property-placeholder location="jdbc.properties" />
    22.     <bean id="jdbcProperties" class="com.spring.demo.pojo.JdbcProperties">
    23.        
    24.         <property name="driver" value="${driver}"/>
    25.         <property name="url" value="${url}"/>
    26.         <property name="username" value="${username}"/>
    27.         <property name="password" value="${password}"/>
    28.     bean>
    29. beans>

    以上,就是注入Property类型。

    1.8、注入null值和空值

    如何注入null值呢???通过【】标签即可注入null值。

    1. <bean id="nullAndEmpty" class="com.spring.demo.pojo.NullAndEmpty">
    2.    
    3.     <property name="name">
    4.         <null/>
    5.     property>
    6. bean>

    如何注入空字符串呢???直接设置【value】属性等于空即可。

    1. <bean id="nullAndEmpty2" class="com.spring.demo.pojo.NullAndEmpty">
    2.    
    3.     <property name="name" value=""/>
    4. bean>

    1.9、注入特殊字符

    在XML文件里面,有时候注入的值存在一些特殊字符(例如:<,>符号),此时XML文件会解析失败,因为(<,>符号)是XML文件规定的标签开始结束标记,我们不能在注入的值里面使用???那要如何解决这个问题呢???

    解决办法:

    • 可以采用【】标签进行转义,这是CDATA是XML规范提供的。

    举例如下:


     以上,就是注入特殊字符的方式。

    二、命名空间

    从第一部分,我们学习了各种数据类型的注入方式,可以看到,每次我们注入一个属性的时候,都需要编写很多的【】标签,很明显,太麻烦了,所以Spring也支持通过命名空间的方式简写注入。下面介绍一下Spring注入的一些命名空间简写方式。

    2.1、p命名空间

    这里说的【p命名空间】是用于简写【】标签注入,使用命名空间,首先需要在XML配置文件里面添加命名空间前缀。

    添加【p命名空间】前缀

    • 需要添加【xmlns:p】开头的前缀。
    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:p="http://www.springframework.org/schema/p"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.        http://www.springframework.org/schema/beans/spring-beans.xsd">
    7. beans>

    p命名空间使用案例

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:p="http://www.springframework.org/schema/p"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.        http://www.springframework.org/schema/beans/spring-beans.xsd">
    7.    
    8.     <bean id="user" class="com.spring.demo.pojo.User"
    9.           p:uid="1001"
    10.           p:username="张三"
    11.           p:password="123456"/>
    12. beans>

    上面,可以看到我们只需要在【】标签里面,采用【p:属性名称】的方式就可以实现注入,而不需要编写【】标签。

    2.2、c命名空间

    这里说的【c命名空间】是用于简写【】标签注入。

    添加【c命名空间】前缀

    • 需要添加【xmlns:c】开头的前缀。
    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:c="http://www.springframework.org/schema/c"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.        http://www.springframework.org/schema/beans/spring-beans.xsd">
    7. beans>

    c命名空间使用

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:c="http://www.springframework.org/schema/c"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.        http://www.springframework.org/schema/beans/spring-beans.xsd">
    7.    
    8.     <bean id="cnamespace" class="com.spring.demo.pojo.Cnamespace" c:_0="1001" c:_1="李四" />
    9.    
    10.     <bean id="cnamespace1" class="com.spring.demo.pojo.Cnamespace" c:id="1001" c:name="李四" />
    11. beans>

    【c命名空间】是构造方法注入的简写,可以两种方式:

    • 根据参数位置注入:【c:_下标位置】
    • 根据参数名称注入:【c:属性名称】

    2.3、util命名空间

    util命名空间,可以将注入集合的那些部分提取出一个公共内容,然后其他地方需要注入的时候,就可以直接引用,而不需要重复的编写。

    添加【util命名空间】前缀

    • 需要添加【xmlns:util】开头的前缀。
    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:util="http://www.springframework.org/schema/util"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.        http://www.springframework.org/schema/beans/spring-beans.xsd
    7.        http://www.springframework.org/schema/util
    8.        http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    9. beans>

    util命名空间的使用


    util命名空间,常用的有如下几个:

    】、【】、【】、【


    综上,这篇文章,详细介绍一下Spring框架中如何注入各种数据类型,包含:注入基本数据类型、数组、集合、Map映射、Property属性、注入空字符串、注入null值、注入特殊字符等内容,以及如何使用命名空间进行依赖注入

  • 相关阅读:
    sql小技巧:日期区间和格式化操作
    Linux的so组件设计框架及逻辑
    SSM - Springboot - MyBatis-Plus 全栈体系(十二)
    Spirng,SpringBoot实现多文件上传(MultipartFile)
    9.7 小结
    PFA晶圆夹在半导体芯片制造中的应用
    nginx修改配置文件不生效
    ACDSee Photo Studio Ultimate 2024特别版(图片编辑器)
    变压器(电抗器) 红外测温作业指导书
    Java21 + SpringBoot3整合springdoc-openapi,自动生成在线接口文档,支持SpringSecurity和JWT认证方式
  • 原文地址:https://blog.csdn.net/yh250648050/article/details/133588717