spring源码分析-----IOC/DI深入源码理解-----深入篇

一,简单案例

package example.pojo;

public class Address {
    private String province;
    private String city;


    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}
package example.pojo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {
    private String name;
    private Address addr;
    private String[] books;
    private List<String> hobbies;
    private Map<String, String> cards;
    private Set<String> games;

    public List <String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List <String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map <String, String> getCards() {
        return cards;
    }

    public void setCards(Map <String, String> cards) {
        this.cards = cards;
    }

    public Set <String> getGames() {
        return games;
    }

    public void setGames(Set <String> games) {
        this.games = games;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public void setAddr(Address addr) {
        this.addr = addr;
    }

    public Address getAddr() {
        return addr;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
<?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="student" class="example.pojo.Student">
        <property name="name" value="张三丰"></property>
        <property name="addr" ref="addr"></property>
        <property name="books">
            <array>
                <value>傲慢与偏见</value>
                <value>仲夏夜之梦</value>
                <value>雾都孤儿</value>
            </array>
        </property>
        <property name="hobbies">
            <list>
                <value>羽毛球</value>
                <value>乒乓球</value>
                <value>玻璃球</value>
                <value>排球</value>
            </list>
        </property>
        <property name="cards">
            <map>
                <entry key="中国银行" value="1545615345415"></entry>
                <entry>
                    <key>
                        <value>农业银行</value>
                    </key>
                    <value>54654861231543</value>
                </entry>
            </map>
        </property>
        <property name="games">
            <set>
                <value>LOL</value>
                <value>dota</value>
                <value>cs</value>
                <value>dnf</value>
                <value>cf</value>

            </set>
        </property>
    </bean>

    <bean id="addr" class="example.pojo.Address">
        <property name="province" value="河南"/>
        <property name="city" value="郑州"/>
    </bean>
</beans>
package example.test;

import example.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Student stu = (Student) ac.getBean("student");
        System.out.println("student(bean)的String类型" + stu.getName());
        System.out.println("student(bean)的实体类类型" + stu.getAddr());
        String[] books = stu.getBooks();
        for (String bk : books) {
            System.out.println("student(bean)的数组类型" + bk);
        }
        List <String> hobbies = stu.getHobbies();
        for (String hb : hobbies) {
            System.out.println("student(bean)的集合list类型" + hb);
        }
        Set <String> games = stu.getGames();
        for (String gs : games) {
            System.out.println("student(bean)的集合set类型" + gs);
        }

        Map <String, String> cards = stu.getCards();
        System.out.println("student(bean)的集合map类型" + cards.get("中国银行") +"==="+ cards.get("农业银行"));

    }
}

结果:

student(bean)的String类型张三丰
student(bean)的实体类类型Address{province='河南', city='郑州'}
student(bean)的数组类型傲慢与偏见
student(bean)的数组类型仲夏夜之梦
student(bean)的数组类型雾都孤儿
student(bean)的集合list类型羽毛球
student(bean)的集合list类型乒乓球
student(bean)的集合list类型玻璃球
student(bean)的集合list类型排球
student(bean)的集合set类型LOL
student(bean)的集合set类型dota
student(bean)的集合set类型cs
student(bean)的集合set类型dnf
student(bean)的集合set类型cf
student(bean)的集合map类型1545615345415===54654861231543

二,理解ioc容器(控制反转)和DI(依赖注入)

2.1.ioc控制反转

就是我们现在不需要去new对象了,需要的话直接通过getbean去获取就行了(主要体现在可配置的文件)。


2.2.DI依赖注入

就是说我们现在假如一个bean依赖另一个bean,比如我们案例中的student依赖address,我们不需要在容器中在去找student的依赖address,这个时候容器已经帮助我们做好了,我们直接获取到student对象的时候直接get.addr就行了。意思就是说容器已经把所有的依赖梳理好了,我们既不需要在去new address对象,也不需要考虑依赖它和student的依赖关系,直接用就行了。

ll

猜你喜欢

转载自www.cnblogs.com/qingruihappy/p/10456779.html