spring DI依赖注入

Posted 市丸银

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring DI依赖注入相关的知识,希望对你有一定的参考价值。

1、官网

https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-dependencies

2、构造器注入(前面有)

3、Set方式注入(重点)

A、依赖 : bean对象 依赖 容器 创建

B、注入: bean对象的属性,由容器注入

https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-factory-properties-detailed

案例

a、pojo

Address.java

package com.wt.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.java

package com.wt.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> games;
    private String wife;
    private Properties info;

    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘‘‘ +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", 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> 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;
    }
}

b、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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.wt.pojo.Address">
        <property name="address" value="徐州大庙李井村"/>
    </bean>

    <bean id="user" class="com.wt.pojo.Student" name="student">
        <!-- 普通数据        -->
        <property name="name" value="吴通"/>
        <!---->
        <property name="address" ref="address"/>
        <!-- 数组        -->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>红楼梦</value>
                <value></value>
                <value>兄弟</value>
            </array>
        </property>
<!--        list-->
        <property name="hobbys">
            <list>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>
<!--        map-->
        <property name="card">
            <map>
                <entry key="银行卡" value="7458694756870"></entry>
                <entry key="电话卡" value="13057122577"></entry>
            </map>
        </property>
<!--        set-->
        <property name="games">
            <set>
                <value>WOW</value>
                <value>鬼泣</value>
            </set>
        </property>
<!--        null-->
        <property name="wife">
            <null/>
        </property>
<!--        Properties-->
        <property name="info">
            <props>
                <prop key="driver">driver</prop>
                <prop key="url">url</prop>
                <prop key="port">3306</prop>
                <prop key="user">root</prop>
            </props>
        </property>
    </bean>

</beans>

c、测试

import com.wt.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyTest {
    @Test
    public void getStudent(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student userInfo = (Student) context.getBean("student");
        System.out.println(userInfo.toString());
    }
}

 

4、其它方式注入

A、p命名空间

property

xml格式 引入p="***"

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

</beans>

例子

   <bean id="user" class="com.wt.pojo.User">
        <property name="name" value="tom"/>
        <property name="age" value="18"/>
    </bean>
    <bean id="user2" class="com.wt.pojo.User" p:name="loss" p:age="34"/>

两个bean标签的效果是一样的

B、c命名空间(有含参构成函数)

构造器注入

xml文件 添加 c="*****"

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

</beans>

例子

    <bean id="user3" class="com.wt.pojo.User">
        <constructor-arg name="name" value="gg"/>
        <constructor-arg name="age" value="45"/>
    </bean>

    <bean id="user4" class="com.wt.pojo.User" c:name="死侍" c:age="100"/>

上述两种情况一致

 

以上是关于spring DI依赖注入的主要内容,如果未能解决你的问题,请参考以下文章

Spring依赖注入(DI)的理解

spring IOC(控制反转)及DI(依赖注入)

spring------控制反转(IOC)/依赖注入(DI)

Spring5依赖注入(DI)

Spring-IOC学习笔记-03DI依赖注入

Spring依赖注入(DI)的三种方式