【Spring】-Spring的IoC和DI-LMLPHP

【Spring】-Spring的IoC和DI-LMLPHP

一、什么是Spring?

1)什么是IoC容器?

1> 理解

//测试类
public class Test {
    public static void main(String[] args) {
        House house = new House();
        house.init();
    }
}
//房
public class House {
    public void init(){
        // 创建房子 依赖于挂房梁等操作
        Beam beam = new Beam();
        beam.init();

        System.out.println("House: House create");
    }
}
//房梁
public class Beam {
    private String material;//(杉木)
    public void init(){
        // 挂房梁等 依赖于 砌好砖
        Brick brick = new Brick();
        brick.init();

        System.out.println("Bean: 房梁木用 " + this.material);
    }
}
//砖
public class Brick {
    private String redBrick;//红砖

    public void init(){
        // 砌砖 依赖于 已经打好地基
        Foundation foundation = new Foundation();
        foundation.init();

        System.out.println("Brick: 砌砖用 " + this.redBrick);
    }
}
//地基
public class Foundation {
    private String rebar;//钢筋
    private String cement;//水泥

    public void init(){
        System.out.println("Foundation: 打地基用 " + this.rebar +" "+ this.cement);
    }
}

上述代码运行结果如下:
【Spring】-Spring的IoC和DI-LMLPHP

2> 法一,传递(存在依赖关系传对象)

//测试类
package demo2;
public class Test {
    //测试传对象 解耦合代码
    public static void main(String[] args) {
        Foundation foundation = new Foundation("xx号钢筋", "xx号水泥");
        Brick brick = new Brick(foundation, "xx号红砖");
        Beam beam = new Beam(brick, "杉木");
        House house = new House(beam);
        house.init();
    }
}

//House类
package demo2;
public class House {
    private Beam beam;
    public House(Beam beam){
        this.beam = beam;
    }
    public void init(){
        // 创建房子 依赖于挂房梁等操作
        beam.init();

        System.out.println("house: 房子建成");
    }
}

//Beam类
package demo2;
public class Beam {
    private Brick brick;
    private String material;

    public Beam(Brick brick, String material){
        this.brick = brick;
        this.material = material;
    }
    public void init(){
        // 挂房梁等 依赖于 砌好砖
        brick.init();

        System.out.println("Bean: 房梁木用 " + this.material);
    }
}

//Brick类
package demo2;
public class Brick {
    private Foundation foundation;
    private String redBrick;//红砖

    public Brick(Foundation foundation, String redBrick){
        // 砌砖 依赖于 已经打好地基
        this.foundation = foundation;
        this.redBrick = redBrick;//新增
    }
    public void init(){
        this.foundation.init();
        System.out.println("Brick: 砌砖用 " + this.redBrick);
    }
}

//Foundation类
package demo2;
public class Foundation {
    private String rebar;//钢筋
    private String cement;//水泥

    public Foundation(String rebar, String cement){
        this.rebar = rebar;
        this.cement = cement;
    }
    public void init(){
        System.out.println("Foundation: 打地基用 " + this.rebar +" "+ this.cement);
    }
}

运行结果:
【Spring】-Spring的IoC和DI-LMLPHP

3> 法二,使用Spring

Spring IoC 核心操作:
Spring IoC 优点:

2)什么是DI?

补充1:实现IoC除了DI之外,还需要控制反转容器(在Spring中就是Spring容器)和配置信息(xml文件,描述对象和依赖关系的元数据)。

补充2:① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩(在这里列出这种序号,需要的自取)。

08-12 09:02