Spring中的IOC和DI(“最易懂得Spring学习”)(三)

六、依赖注入DI


1.构造器注入


前面的hello就是构造器注入


2.Set方式注入【重点】


  • 依赖注入:Set注入!


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


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


2.1【环境搭建】


1.复杂类型

package com.hxl.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2.真实测试对象

package com.hxl.pojo;

import java.util.*;

public class Student {
    private String name;
    //引用类型,到时候用ref赋值
    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 +
                '}';
    }
}


3.beans.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">

    <!--第一种:普通值注入:value-->
    <bean id="student" class="com.hxl.pojo.Student">
        <property name="name" value="王木木"/>
    </bean>

</beans>

4.测试类

import com.hxl.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

2.2 示例

<?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.hxl.pojo.Address"/>


    <bean id="student" class="com.hxl.pojo.Student">
        <!--第一种:普通值注入:value-->
        <property name="name" value="王木木"/>
        <!--第二种,Bean注入:ref-->
        <property name="address" ref="address"/>
        <!--第三种,数组注入-->
        <property name="books">
            <array>
                <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="1254482515165451X"/>
                <entry key="银行卡" value="1549413214151515485"/>
            </map>
        </property>
        <!--第六种:Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>王者荣耀</value>
            </set>
        </property>
        <!--第七种:NULL-->
        <property name="wife">
            <null/>
        </property>
        <!--第八种:Properties-->
        <property name="info">
            <props>
                <prop key="学号">yj2021</prop>
                <prop key="姓名">王木木</prop>
            </props>
        </property>
    </bean>
</beans>
import com.hxl.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
        /*
        * Student{name='王木木', address=Address{address='null'}, books=[红楼梦, 水浒传], hobbys=[谈恋爱, 敲代码],
        * card={身份证=1254482515165451X, 银行卡=1549413214151515485}, games=[LOL, 王者荣耀], wife='null',
        *  info={学号=yj2021, 姓名=王木木}}

         * */
    }
}


运行之前记得在address中加入toString方法,以及在Student中的toString中加入address.toString()

上一篇:在k8s中安装flannel的故障解决: Failed to create SubnetManager: error retrieving pod spec for : the server does not allow access to the requested resource


下一篇:如何用 RocketMQ 打造金融级消息服务平台?微众银行这么做