• 【Spring】DI依赖注入


    DI

    在Spring里面分为构造器注入set方式注入
    也就是把bean的属性完善一下

    • 我们要强调一下,DI只是IOC的一种方式,IOC不一定要通过Spring里面的DI实现,也可以通过非Spring的写法实现,比如说让用户自己把实现好的类丢进去之类的

    • 依赖:bean对象的创建依赖于容器

    • 注入:bean对象中的所有属性,都由容器来注入
      不管你用不用配置里面的bean,它总是存在那里,你用的话也是从里面取(通过反射)

    复杂object的多数据结构的di

    复杂对象如下:

    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 + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    package com.pojo;
    
    import java.util.*;
    
    public class Student {
    
        private String name;
        private Address address;
    
        private String[] books;
        private List<String> hobbies;
    
        private Map<String, String> card;
        private Set<String> game;
    
        private Properties infor;
        private String wife;
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", address=" + address +
                    ", books=" + Arrays.toString(books) +
                    ", hobbies=" + hobbies +
                    ", card=" + card +
                    ", game=" + game +
                    ", infor=" + infor +
                    ", wife='" + wife + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    多种数据结构的注入

    包括ref注入,数组[]注入,list注入, map注入, set注入, null注入, Property注入

    
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="address" class="com.pojo.Address">
            <property name="address" value="xian">property>
        bean>
    
        <bean id="student" class="com.pojo.Student">
            <property name="name" value="hou"/>
            <property name="address" ref="address"/>
    
            
            <property name="books">
                <array>
                    <value>三国value>
                    <value>西游value>
                    <value>水浒value>
                array>
            property>
    
            
            <property name="hobbies">
                <list>
                    <value>eatvalue>
                    <value>drinkvalue>
                    <value>playvalue>
                list>
            property>
    
            <property name="card">
                <map>
                    <entry key="1" value="12"/>
                    <entry key="2" value="23"/>
                map>
            property>
    
            <property name="game">
                <set>
                    <value>wangzhevalue>
                    <value>daotavalue>
                    <value>lolvalue>
                set>
            property>
    
            <property name="wife">
                <null>null>
            property>
    
            
            <property name="infor">
                <props>
                    <prop key="id">20200405prop>
                    <prop key="name">hdkprop>
                props>
            property>
        bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    第三方的p标签和c标签

    pojo:

    package com.pojo;
    
    public class User {
    
        private String name;
        private int age;
    
        public User() {
        }
    
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    使用p标签和c标签要在头文件引用好
    p注入就是set注入,c注入就是构造器注入

    
    <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"
           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">
    
        
        <bean id="use" class="com.pojo.User" p:name="dong" p:age="10">
        bean>
    
        
        <bean id="use2" class="com.pojo.User" c:name="kun" c:age="19">bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    bean的作用域

    在这里插入图片描述
    1.单例模式(默认):一直都是同一个对象(不适用于某些并发场景)

    <bean id="use2" class="com.pojo.User" c:name="kun" c:age="19" scope="singleton">bean>
    
    • 1

    2.原型模式: 每次从容器中get的时候,都产生一个新对象!

    <bean id="use2" class="com.pojo.User" c:name="kun" c:age="19" scope="prototype">bean>
    
    • 1

    3.其余的request、session、application这些只能在web开放中使用!

    总结

    在总结完bean的注入方式后,这里又总结了不同类型数据结构的注入
    后面的话继续总结自动装配,还有autowird注解的使用,这些才是重点
    因为springboot中全是注解

  • 相关阅读:
    DDD领域驱动设计-值对象
    儿童玩具出口欧盟CE认证测试标准
    第23章_瑞萨MCU零基础入门系列教程之ADC与DSP
    内网渗透信息收集
    【XSS_MSN】基于GCN的web攻击Payload识别与可解释性分析
    入门正则表达式
    热力图生成算法及其具体实现
    STM32内部flash详解(1)
    cpu门禁电梯卡复制测试过程
    element 二次确认框,内容自定义处理
  • 原文地址:https://blog.csdn.net/weixin_40986490/article/details/126948522