Spring中依赖注入方式

本文详细介绍了Spring Boot中的依赖注入技术,包括构造器注入、set方法注入,以及使用p和c命名空间的拓展注入。实例演示了如何在实体类中应用这些技巧,并展示了如何为没有无参构造函数的类进行命名空间注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从名字上理解,所谓依赖注入,即组件之间的依赖关系由容器在运行期决定,形象的来说,即由容器动态的将某种依赖关系注入到组件之中。

一、构造器注入
之前讲过,点击这里。

二、set注入 (重点)
注意:要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 是 is。
实体类:

package com.liu.pojo;

/**
 * @author liucong
 * @date 2020/10/6 - 16:40
 */
public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
package com.liu.pojo;

import java.util.*;

/**
 * @author liucong
 * @date 2020/10/6 - 16:39
 */
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

1.普通值注入

<bean id="student" class="com.liu.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="小葱"/>
</bean>

2.Bean注入

<bean id="address" class="com.liu.pojo.Address">
        <property name="address" value="西安"/>
</bean>
<bean id="student" class="com.liu.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="小葱"/>

        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"/>
</bean>

3.数组注入

<property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
</property>

4.List注入

<property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
</property>

5.Map注入

<property name="card">
            <map>
                <entry key="身份证" value="414566988564"/>
                <entry key="银行卡" value="145214521456"/>
            </map>
</property>

6.set注入

<property name="games">
            <set>
                <value>LOL</value>
                <value>BOB</value>
                <value>COC</value>
            </set>
</property>

7.null注入

<property name="wife">
            <null/>
</property>

8.properties注入

<property name="info">
            <props>
                <prop key="学号">20200525</prop>
                <prop key="性别"></prop>
                <prop key="姓名">小明</prop>
            </props>
</property>

三、拓展注入实现

实体类
注意:没有有参构造函数

package com.liu.pojo;

/**
 * @author liucong
 * @date 2020/10/6 - 20:08
 */
public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

1、p命名空间注入
需要在头文件中加入约束文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.liu.pojo.User" p:name="小葱" p:age="18"/>

2、c 命名空间注入
需要在头文件中加入约束文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.liu.pojo.User" c:name="liucong" c:age="19" scope="prototype"/>

注意:在实体类中需要加入有参构造函数,否则会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值