4、Spring之依赖注入

这篇具有很好参考价值的文章主要介绍了4、Spring之依赖注入。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

依赖注入就是对类的属性进行赋值

4.1、环境搭建

创建名为spring_ioc_xml的新module,过程参考3.1节

4.1.1、创建spring配置文件

4、Spring之依赖注入

<?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">

</beans>

4.1.2、创建学生类Student

4、Spring之依赖注入

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/27 - 22:33
 */
public class Student {

    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

4.2、setter注入(常用)

4.2.1、配置bean

4、Spring之依赖注入

    <!--
        property标签:通过组件类的setXxx()方法给组件对象设置属性
            name属性:指定属性名
            value属性:设置属性值
    -->
    <bean id="student" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
    </bean>

4.2.2、测试

4、Spring之依赖注入

package org.rain.spring.test;

import org.junit.Test;
import org.rain.spring.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author liaojy
 * @date 2023/7/27 - 22:43
 */
public class IOCByXmlTest {

    @Test
    public void testDISetter(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("student", Student.class);
        System.out.println(student);
    }
}

4.3、构造器注入

4.3.1、配置bean

4、Spring之依赖注入

注意:constructor-arg标签的数量,必须和某一个构造器方法的参数数量一致

    <!--
        constructor标签:通过组件类的有参构造方法给组件对象设置属性
            name属性:指定有参构造方法参数名
            value属性:设置有参构造方法参数值
    -->
    <bean id="studentTwo" class="org.rain.spring.pojo.Student">
        <constructor-arg name="id" value="1002"></constructor-arg>
        <constructor-arg name="name" value="李四"></constructor-arg>
        <constructor-arg name="age" value="24"></constructor-arg>
        <constructor-arg name="sex" value="女"></constructor-arg>
    </bean>

4.3.2、测试

4、Spring之依赖注入

    @Test
    public void testDIConstructor(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentTwo", Student.class);
        System.out.println(student);
    }

4.4、特殊值处理

4.4.1、null值

4.4.1.1、配置bean

4、Spring之依赖注入

注意:该<property name="sex" value="null">写法,实际为sex所赋的值是字符串null

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <property name="name" value="张三"></property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

4.4.1.2、测试

4、Spring之依赖注入

由控制台日志可知,此时sex的值为null

    @Test
    public void testDIspecial(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentThree", Student.class);
        System.out.println(student.getSex().toString());
    }

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

4、Spring之依赖注入

由控制台日志可知,此时age的值也为null;所以不配置属性也能实现同样的效果

4.4.2、xml字符值

4.4.2.1、方式一:实体

4、Spring之依赖注入

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <!--用实体来代替xml字符-->
        <property name="name" value="&lt;张三&gt;"></property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

4.4.2.2、方式二:CDATA节

4、Spring之依赖注入

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <!-- CDATA中的C代表Character,是文本、字符的含义,CDATA就表示纯文本数据 -->
        <!-- XML解析器看到CDATA节就知道这里是纯文本,就不会当作XML标签或属性来解析 -->
        <!-- 所以CDATA节中写什么符号都随意 -->
        <property name="name">
            <value><![CDATA[<张三>]]></value>
        </property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

4.4.2.3、测试

4、Spring之依赖注入

4.5、为类类型的属性赋值

4.5.1、方式一:外部bean(常用)

4.5.1.1、创建班级类Clazz

4、Spring之依赖注入

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/28 - 7:54
 */
public class Clazz {
    private Integer cid;
    private String cname;

    public Clazz() {
    }

    public Clazz(Integer cid, String cname) {
        this.cid = cid;
        this.cname = cname;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Clazz{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}

4.5.1.2、修改Student类

4、Spring之依赖注入

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/27 - 22:33
 */
public class Student {

    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    private Clazz clazz;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, String sex, Clazz clazz) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.clazz = clazz;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", clazz=" + clazz +
                '}';
    }
}

4.5.1.3、配置外部bean

4、Spring之依赖注入

    <bean id="clazz" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
    </bean>

4.5.1.4、引用外部bean

4、Spring之依赖注入

    <bean id="studentfour" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
        <property name="clazz" ref="clazz"></property>
    </bean>

4.5.1.5、测试

4、Spring之依赖注入

    @Test
    public void testDIOuterBean(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentfour", Student.class);
        System.out.println(student);
    }

4.5.2、方式二、级联

4.5.2.1、配置bean

4、Spring之依赖注入

    <bean id="clazz" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
    </bean>


    <bean id="studentFive" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <!-- 一定先引用某个bean为属性赋值,才可以使用级联方式更新属性 -->
        <property name="clazz" ref="clazz"></property>
        <property name="clazz.cid" value="2222"></property>
        <property name="clazz.cname" value="五年A班"></property>
    </bean>

4.5.2.2、测试

4、Spring之依赖注入

    @Test
    public void testDICascade(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentFive", Student.class);
        System.out.println(student);
    }

4.5.3、内部bean(常用)

4.5.3.1、配置bean

4、Spring之依赖注入

    <bean id="studentSix" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
    </bean>

4.5.3.2、测试

4、Spring之依赖注入

    @Test
    public void testDIInnerBean(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentSix", Student.class);
        System.out.println(student);
    }

4.6、为数组类型的属性赋值

4.6.1、修改Student类

4、Spring之依赖注入

在Student类中添加或修改以下代码:

    private String hobby[];

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", clazz=" + clazz +
                ", hobby=" + Arrays.toString(hobby) +
                '}';
    }

4.6.2、配置bean

4、Spring之依赖注入

    <bean id="studentSeven" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <!--如果数组类型是基本类型和string类型,就用value标签-->
                <!--如果数组类型是类类型,就用ref标签-->
                <value>游泳</value>
                <value>跑步</value>
                <value>骑车</value>
            </array>
        </property>
    </bean>

4.6.3、测试

4、Spring之依赖注入

    @Test
    public void testDIArray(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentSeven", Student.class);
        System.out.println(student);
    }

4.7、为List集合类型的属性赋值

4.7.1、修改Clazz类

4、Spring之依赖注入

在Clazzt类中添加或修改以下代码:


    private List<Student> students;

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }


    @Override
    public String toString() {
        return "Clazz{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", students=" + students +
                '}';
    }


4.7.2、配置bean

4、Spring之依赖注入

    <bean id="student" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
    </bean>

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <property name="name">
            <value><![CDATA[<张三>]]></value>
        </property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

    <bean id="clazz" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
    </bean>

    <bean id="studentfour" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz" ref="clazz"></property>
    </bean>

    <bean id="clazzTwo" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
        <property name="students">
            <list>
                <!--如果list集合元素是基本类型和string类型,就用value标签-->
                <!--如果list集合元素是类类型,就用ref标签-->
                <ref bean="student"></ref>
                <ref bean="studentThree"></ref>
                <ref bean="studentfour"></ref>
            </list>
        </property>
    </bean>

4.7.3、测试

4、Spring之依赖注入

    @Test
    public void testDIList(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Clazz clazz = applicationContext.getBean("clazzTwo", Clazz.class);
        System.out.println(clazz);
    }

4.8、为Map集合类型的属性赋值

4.8.1、创建教师类Teacher

4、Spring之依赖注入

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/30 - 12:10
 */
public class Teacher {
    private Integer tid;
    private String tname;

    public Teacher() {
    }

    public Teacher(Integer tid, String tname) {
        this.tid = tid;
        this.tname = tname;
    }

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                '}';
    }
}

4.8.2、修改Student类

4、Spring之依赖注入

在Student类中添加或修改以下代码:

    private Map<String,Teacher> teacherMap;

    public Map<String, Teacher> getTeacherMap() {
        return teacherMap;
    }

    public void setTeacherMap(Map<String, Teacher> teacherMap) {
        this.teacherMap = teacherMap;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", clazz=" + clazz +
                ", hobby=" + Arrays.toString(hobby) +
                ", teacherMap=" + teacherMap +
                '}';
    }

4.8.3、配置bean

4、Spring之依赖注入

    <bean id="teacherOne" class="org.rain.spring.pojo.Teacher">
        <property name="tid" value="10010"></property>
        <property name="tname" value="张老师"></property>
    </bean>

    <bean id="teacherTwo" class="org.rain.spring.pojo.Teacher">
        <property name="tid" value="10086"></property>
        <property name="tname" value="李老师"></property>
    </bean>

    <bean id="studentEight" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <value>游泳</value>
                <value>跑步</value>
                <value>骑车</value>
            </array>
        </property>
        <property name="teacherMap">
            <map>
                <!--如果键是字面量,就用key属性,否则用key-ref属性 -->
                <!--如果值是字面量,就用value属性,否则用value-ref属性 -->
                <entry key="10010" value-ref="teacherOne"></entry>
                <entry key="10086" value-ref="teacherTwo"></entry>
            </map>
        </property>
    </bean>

4.8.4、测试

4、Spring之依赖注入

    @Test
    public void testDIMap(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentEight", Student.class);
        System.out.println(student);
    }

4.9、为集合类型的属性赋值(解耦引用方式)

4.9.1、配置bean

4、Spring之依赖注入

注意:使用util:list、util:map标签必须引入相应的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" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--list集合类型的bean-->
    <util:list id="students">
        <ref bean="student"></ref>
        <ref bean="studentThree"></ref>
        <ref bean="studentfour"></ref>
    </util:list>

    <!--map集合类型的bean-->
    <util:map id="teacherMap">
        <entry key="10010" value-ref="teacherOne"></entry>
        <entry key="10086" value-ref="teacherTwo"></entry>
    </util:map>

    <bean id="studentNine" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <value>游泳</value>
                <value>跑步</value>
                <value>骑车</value>
            </array>
        </property>
        <property name="teacherMap" ref="teacherMap"> </property>
    </bean>

</beans>

4.9.2、测试

4、Spring之依赖注入

    @Test
    public void testDIUtil(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentNine", Student.class);
        System.out.println(student);
    }

4.10、p命名空间(了解)

4.10.1、配置bean

4、Spring之依赖注入

注意:p命名空间的依赖注入,本质上是属性方式(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" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--map集合类型的bean-->
    <util:map id="teacherMap">
        <entry key="10010" value-ref="teacherOne"></entry>
        <entry key="10086" value-ref="teacherTwo"></entry>
    </util:map>

    <bean id="studentTen" class="org.rain.spring.pojo.Student"
    p:id="1001" p:name="张三" p:teacherMap-ref="teacherMap"></bean>

</beans>

4.10.2、测试

4、Spring之依赖注入

    @Test
    public void testDIP(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentTen", Student.class);
        System.out.println(student);
    }

4.11、引入外部属性文件

4.11.1、引入数据库相关依赖

4、Spring之依赖注入

        <!-- MySQL驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!-- 数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.31</version>
        </dependency>

4.11.2、创建外部属性文件

4、Spring之依赖注入

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

4.11.3、配置bean

4、Spring之依赖注入

注意:使用context:property-placeholder标签必须引入相应的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入外部属性文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--通过${key}的方式访问外部属性文件的value-->
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

</beans>

4.11.4、测试

4、Spring之依赖注入文章来源地址https://www.toymoban.com/news/detail-622121.html

    @Test
    public void testDIOuterFile() throws SQLException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-datasource");
        DruidDataSource datasource = applicationContext.getBean("datasource", DruidDataSource.class);
        System.out.println(datasource.getConnection());
    }

到了这里,关于4、Spring之依赖注入的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • quarkus依赖注入之十一:拦截器高级特性上篇(属性设置和重复使用)

    这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇是《quarkus依赖注入》系列的第十一篇,之前的[《拦截器》]学习了拦截器的基础知识,现在咱们要更加深入的了解拦截器,掌握两种高级用法:拦截器属性和重复使用拦截器 先来回顾拦截器的基

    2024年02月13日
    浏览(25)
  • Spring面试整理-Spring的依赖注入

    Spring框架的依赖注入(DI)是其核心功能之一,它允许对象定义它们依赖的其他对象,而不是自己创建或查找它们。这种机制促进了松耦合和更容易的测试。 依赖注入是一种设计模式,其中一个对象或方法提供另一个对象的依赖关系。在Spring中,这些依赖通常是服务、配置值

    2024年01月19日
    浏览(49)
  • Spring:泛型依赖注入

    泛型 :具有占位符(类型参数)的类、结构、接口和方法,通过 的方式定义了一个形式参数,在实例化时再指明具体类型 依赖注入 :IoC 的具体实现,指对象之间的依赖关系在程序运行时由外部容器动态的注入依赖行为方式 泛型依赖注入 :在进行依赖注入的同时,使用泛型

    2024年02月15日
    浏览(27)
  • 4、Spring之依赖注入

    依赖注入就是对类的属性进行赋值 创建名为spring_ioc_xml的新module,过程参考3.1节 注意:constructor-arg标签的数量,必须和某一个构造器方法的参数数量一致 4.4.1.1、配置bean 注意:该property name=\\\"sex\\\" value=\\\"null\\\"写法,实际为sex所赋的值是字符串null 4.4.1.2、测试 由控制台日志可知,

    2024年02月14日
    浏览(23)
  • Spring 的依赖注入

    @ 目录 Spring 的依赖注入 每博一文案 1. 依赖注入 1.1 构造注入 1.1.1 通过参数名进行构造注入 1.1.2 通过参数的下标,进行构造注入 1.1.3 不指定参数下标,不指定参数名字,通过自动装配的方式 1.2 set 注入 2. set注入的各种方式详解 2.1 set 注入外部Bean 2.2 set 注入内部Bean 2.3 set 注

    2024年02月16日
    浏览(39)
  • Spring 依赖注入详解

    目录 1、基于构造器的依赖注入 2、基于 Setter 方法的依赖注入 3、使用构造器注入还是 setter 方法注入? 4、依赖注入解析的过程 5、依赖注入的相关示例 // 依赖关系,指的就是对象之间的相互协作关系         依赖注入(DI)是一个过程,在这个过程中,对象仅通过构造函

    2024年02月06日
    浏览(40)
  • Spring Boot中的依赖注入和自动注入

    以下内容为本人学习 Spring Boot的依赖注入和自动注入 与ChatGpt提问后对其回答 进行部分修改 (有的错误实在是离谱 = =)、格式调整等操作后的答案, 可能对于其中部分细节(是错是对,能力有限有的看不出来 = =),并未做深入探究 ,大家感兴趣的话可以自行验证。 依赖注

    2024年02月06日
    浏览(35)
  • Spring 02 -Spring依赖注入+Spring注解开发

    依赖注入:在Spring创建对象的同时,为其属性赋值,称之为依赖注入。 创建对象时,Spring工厂会通过Set方法为对象的属性赋值。 范例:定义一个Bean类型 属性注入 范例:定义一个Bean类型 提供构造方法 构造方法注入 复杂类型指的是:list、set、map、array、properties等类型 定义

    2023年04月09日
    浏览(35)
  • Spring《三》DI 依赖注入

    🍎道阻且长,行则将至。🍓 上一篇:Spring《二》bean 的实例化与生命周期 下一篇:敬请期待 向一个类中传递数据的方式有: 普通方法(set 方法) 和 构造方法 。Spring 就相对应地为我们提供了两种注入方式: setter 注入 和 构造器注入 。同时也包括简单类型和引用类型(对象

    2023年04月21日
    浏览(36)
  • Spring 的依赖注入(DI)

    欢迎来到本篇文章,书接上回,本篇说说 Spring 中的依赖注入,包括注入的方式,写法,该选择哪个注入方式以及可能出现的循环依赖问题等内容。 如果正在阅读的朋友还不清楚什么是「依赖」,建议先看看我第一篇文章,通过 Employee 和 Department 简单说了什么是所谓的依赖。

    2024年02月11日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包