Loading

Spring DI(依赖注入)

1.依赖注入(DI)

  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

1.1 构造器注入

    <bean id="user2" class="com.zhang.pojo.User">
        <constructor-arg index="0" value="zhang" />
        <constructor-arg index="1" value="18" />
    </bean>

1.2 Set方式注入(重点)

依赖注入:set注入

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

注入:bean对象中所有的属性,由容器来注入

普通值注入,基于set注入

环境搭建

Address类

package com.zhang.pojo;

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 + '\'' +
                '}';
    }
}

Student类

package com.zhang.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String ,String > card;
    private Set<String> game;
    private String wife;
    private Properties info;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", game=" + game +
                ", wife='" + wife + '\'' +
                ", info=" + 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> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

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

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

    public Set<String> getGame() {
        return game;
    }

    public void setGame(Set<String> game) {
        this.game = game;
    }

    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;
    }
}

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">
    <bean id="address" class="com.zhang.pojo.Address">
        <property name="address" value="北京中南海"/>
    </bean>
    <bean id="student" class="com.zhang.pojo.Student">

    </bean>
</beans>

1.2.1 常量注入

        <!--第一种:普通值注入,value-->
        <property name="name" value="cnfalltime" />

1.2.2 数组注入

        <!--第二种:数组注入,array-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
            </array>
        </property>

1.2.3 列表注入

        <!--第三种:List注入,list-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>

1.2.4 map注入

        <!--第四种:Map注入,map-->
        <property name="card">
            <map>
                <entry key="身份证" value="141414144144414141" />
                <entry key="银行卡" value="1234567891234567891" />
                <entry key="电话号" value="12345698765" />
            </map>
        </property>

1.2.5 set注入

        <!--第五种:Set注入,value-->
        <property name="game">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>

1.2.6 null注入

        <!--第六种:null注入,value-->
<!--        <property name="wife" value="" />-->//方式1
        <property name="wife">//方式二
            <null></null>
        </property>

1.2.7 properties注入

        <!--第七种:Properties注入,props-->
        <property name="info">
            <props>
                <prop key="driver">com.jdbc.cj.JDBC</prop>
                <prop key="url">localhost:3306</prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

1.2.8 bean注入(引用)

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

完善注入信息

1.3 拓展方式注入

p命名和c命名不能直接使用,需要在头部导入xml约束

<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">
</beans>
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
package com.zhang.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;
    }
}

p命名注入 property

    <!--p命名空间注入可以直接注入简单属性的值(相当于property注入)-->
    <bean id="user" class="com.zhang.pojo.User" p:name="zhang" p:age="18" />

c命名空间注入 constructor

    <!--c命名空间注入可以直接注入简单属性的值(相当于构造器注入)-->
    <bean id="user" class="com.zhang.pojo.User" c:_0="CnFallTime" c:_1="22" />

注意点:p命名和c命名不能直接使用,需要导入xml约束

posted @ 2022-03-26 21:43  Cn_FallTime  阅读(8)  评论(0编辑  收藏  举报