当前位置: 移动技术网 > IT编程>开发语言>Java > Spring:依赖注入(DI)学习笔记

Spring:依赖注入(DI)学习笔记

2020年07月27日  | 移动技术网IT编程  | 我要评论

Spring:依赖注入(DI)学习笔记

什么是依赖注入?

依赖注入(DI)从名字来看分为两部分:
依赖: 可以理解为bean对象的创建依赖于Spring容器。
注入: 可以理解为bean对象的所有属性,通过Spring容器注入。

依赖注入的方式

Set注入

通过类中属性的set方法注入属性的值

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> 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> 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> 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) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

    <bean id="address" class="com.huang.pojo.Address">
        <property name="address" value="地址"/>
    </bean>

    <bean id="student" class="com.huang.pojo.Student">
        <!--普通值注入-->
        <property name="name" value="姓名"/>
        <!--bean注入,ref-->
        <property name="address" ref="address"/>
        <!--数组-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
        <!--List集合-->
        <property name="hobbys">
            <list>
                <value>吃饭</value>
                <value>喝酒</value>
                <value>抽烟</value>
            </list>
        </property>
        <!--Map集合-->
        <property name="card">
            <map>
                <entry key="身份证" value="123456789"/>
                <entry key="学生卡" value="987654321"/>
            </map>
        </property>
        <!--Set集合-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
            </set>
        </property>
        <!--空值注入-->
        <property name="wife">
            <null/>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">123123213</prop>
                <prop key="性别"></prop>
                <prop key="driver">asdasd</prop>
                <prop key="url">asdasd</prop>
            </props>
        </property>
    </bean>

构造器注入

通过类的有参构造器注入属性值

public class User {
    private int age;
    private String name;

    public User(){

    }

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }
}
    <bean id="user" class="com.huang.pojo.User">
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="name" value="姓名"/>
    </bean>

c命名,p命名空间注入

使用这两种命名空间注入需要导入头:

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

p命名空间注入property,作用同property,只是更简便。

 <bean id="user" class="com.huang.pojo.User" p:age="21" p:name="姓名"/>

c命名就像构造器注入

<bean id="user2" class="com.huang.pojo.User" c:age="18" c:name="老八"/>

本文地址:https://blog.csdn.net/weixin_45730220/article/details/107593444

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网