02、Spring框架入门:SpringIOC的实现-5000字匠心出品

SpringIOC的实现

    • 1.IOC解释
  • 2.代码实现
  • 3.使用无参构造器实现IOC
  • 4.使用有参构造器实现IOC
  • 5.使用工厂模式实现IOC

责任链:

  • 我们使用mvc进行开发的时候,数据在各层之间进行传递,数据在业务上构成一个链条,这个链条称之为责任链

基于责任链开发的缺点:

  • 基于责任链开发模式,我们发现代码层和层之间相互调用,造成了层和层的耦合性太高了
  • 我们写代码的时候讲究的原则–低耦合 高内聚

解决的方案:

  • Spring IOC : 控制反转
    控制:就是指的的Spring创建对象的过程
    反转:创建对象这个操作本身是程序员自己做的,现在反交给Spring IOC进行创建

1.IOC解释

  • 英文名称:(Inversion of Control)
  • 中文名称:控制反转
    控制:对类实例化的控制.指的是创建对象这个事情
    反转:以前由程序员实例化的过程,转交给Spring进行实例化
  • 通俗解释:由Spring帮助实例化对象,释放程序员对对象的关注度,所有的关注度都放在业务上.同时还可以达到解耦的效果
  • 4.项目应用场景:
    帮助创建SqlSessionFactory
    管理所有Mapper接口,管理所有ServiceImpl类.

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">
    <!--Student  stu =new Student()-->
    <bean id="stu" class="com.spring1.Student"></bean>
</beans>

package com.spring1;

public class Student {
   
     

    public Student() {
   
     
        System.out.println("--对象创建--");
    }

    public void eat() {
   
     
        System.out.println("eat()----");
    }
}

package com.spring1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
   
     
    public static void main(String[] args) {
   
     
        //Student  stu =new Student();
        //解析xml文件
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获得对象
        //Student  stu = (Student) app.getBean("stu");
        Student stu1 = app.getBean("stu", Student.class);
        Student stu2 = app.getBean("stu", Student.class);
        System.out.println(stu1 == stu2);
    }
}

运行结果:
*
Spring IOC的好处:

  • 实现了各层之间的解耦

3.使用无参构造器实现IOC

<bean id="stu2" class="com.spring2.Student">

4.使用有参构造器实现IOC

    <!--
       Student  stu3=new Student(18,zs,男);
       注意
         [1]name属性和形参的名称保持一致的
         [2]形参的顺序不用和标签的顺序一致
         [3]我们使用name属性进行调用
            但是除了name属性还有index(从0)
            type :数据类型
            建议三者都写上即可
    -->
    <bean id="stu3" class="com.spring2.Student">
        <constructor-arg name="a" value="18"></constructor-arg>
        <constructor-arg name="name" value="zs"></constructor-arg>
        <constructor-arg name="sex" value="男"></constructor-arg>

        <!--<constructor-arg name="a" index="1" type="int" value="123"></constructor-arg>
        <constructor-arg name="b" index="0" type="java.lang.String" value="456"></constructor-arg>-->
    </bean>

package com.spring2;

public class Student {
   
     
    private String name;
    private String sex;
    private int age;

    public String getName() {
   
     
        return name;
    }

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

    public String getSex() {
   
     
        return sex;
    }

    public void setSex(String sex) {
   
     
        this.sex = sex;
    }

    public int getAge() {
   
     
        return age;
    }

    public void setAge(int age) {
   
     
        this.age = age;
    }

    public Student(String name, String sex, int a) {
   
     
        this.name = name;
        this.sex = sex;
        this.age = a;
    }

    public Student(String b, int a) {
   
     
        this.name = b;
        this.age = a;
    }

    public Student(int b, String a) {
   
     
        /*this.name = b;
        this.age = a;*/
    }

    public Student() {
   
     
        System.out.println("student对象被建立");
    }

    @Override
    public String toString() {
   
     
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.spring2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
   
     
    public static void main(String[] args) {
   
     
        //解析xml
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu3 = app.getBean("stu3", Student.class);
        System.out.println(stu3);
    }
}

运行结果:
*

5.使用工厂模式实现IOC

  • 设计模式:设计模式是为了解决某一类问题的产生
  • 工厂模式就是批量生产对象的
    <!--【3】使用工厂模式创建对象-->
    <!-- Factory  factory=new Factory();-->
    <bean id="factory" class="com.spring3.Factory"></bean>
    <!-- factory.getInstance("tea");-->
    <bean id="be" factory-bean="factory" factory-method="getInstance">
        <constructor-arg name="param" value="tea"></constructor-arg>
    </bean>
    <!--Factory.getInstance2('stu')-->
    <bean id="be2" class="com.spring3.Factory" factory-method="getInstance2">
        <constructor-arg name="param" value="stu"></constructor-arg>
    </bean>

package com.spring3;

public interface People {
   
     
    public    void  eat();
    public   void   run();
}

package com.spring3;

public class Factory {
   
     

    public People getInstance(String param) {
   
     
        if ("stu".equals(param)) {
   
     
            return new Student();
        } else if ("tea".equals(param)) {
   
     

            return new Teacher();
        }
        return null;
    }

    public static People getInstance2(String param) {
   
     
        if ("stu".equals(param)) {
   
     
            return new Student();
        } else if ("tea".equals(param)) {
   
     
            return new Teacher();
        }
        return null;
    }
}

package com.spring3;

public class Student implements People {
   
     

    public Student() {
   
     
        System.out.println("学生对象创建");
    }

    @Override
    public void eat() {
   
     
        System.out.println("学生吃的方法");
    }

    @Override
    public void run() {
   
     
        System.out.println("学生跑的方法");
    }
}

package com.spring3;

public class Teacher implements People {
   
     

    public Teacher() {
   
     
        System.out.println("老师对象创建");
    }

    @Override
    public void eat() {
   
     
        System.out.println("老师吃的方法");
    }

    @Override
    public void run() {
   
     
        System.out.println("老师跑的方法");
    }
}

package com.spring3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
   
     

    public static void main(String[] args) {
   
     
        /*Factory  factory =new Factory();
        People stu = factory.getInstance("stu");*/
        /*People stu = Factory.getInstance2("stu");*/
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = app.getBean("be", People.class);
        people.eat();
    }
}

运行结果:
*