Java Spring framework: the implementation of SpringIOC-5000 words

Posted by jase01 on Fri, 26 Jun 2020 05:18:14 +0200

Implementation of SpringIOC


Chain of responsibility:

  • When we use mvc for development, data is transferred between different layers. Data forms a chain in business, which is called responsibility chain

Disadvantages of development based on responsibility chain:

  • Based on the development mode of responsibility chain, we find that the code layer and layer call each other, which results in the high coupling of layer and layer
  • The principle we pay attention to when we write code - low coupling and high cohesion

Solution:

  • Spring IOC: inversion of control
    Control: refers to the process of creating objects in Spring
    Reverse: the operation of creating objects itself is done by the programmer himself. Now, it's back to Spring IOC to create

1.IOC interpretation

  • English Name: (Inversion of Control)
  • Chinese Name: inversion of control
    Control: control over class instantiation. It refers to the creation of objects
    Inversion: the process previously instantiated by programmers is transferred to Spring for instantiation
  • Popular explanation: Spring helps to instantiate the object, release the programmer's attention to the object, and put all the attention on the business. At the same time, it can achieve the decoupling effect
  • 4. Project application scenario:
    Help create SqlSessionFactory
    Manage all Mapper interfaces and all ServiceImpl classes

2. Code implementation

Guide Package:

<?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("--objects creating--");
    }

    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();
        //Parsing xml files
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //Get object
        //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);
    }
}

Operation result:

Benefits of Spring IOC:

  • The decoupling between different layers is realized

3. Realize IOC with parameterless constructor

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

4. Implement IOC with parameter constructor

    <!--
       Student stu3 = new student (18, ZS, male);
       be careful
         [1] The name attribute is consistent with the name of the parameter
         [2] The order of parameters does not need to be the same as the order of labels
         [3] We use the name property to call
            But in addition to the name attribute, there is index (from 0)
            Type: data type
            It is recommended to write all three
    -->
    <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 = "male" >

        <!--<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 Object is created");
    }

    @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) {
        //Parsing xml
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu3 = app.getBean("stu3", Student.class);
        System.out.println(stu3);
    }
}

Operation result:

5. Use factory mode to realize IOC

  • Design pattern: design pattern is to solve a certain kind of problem
  • Factory mode is the object of mass production
    <!--[3]Creating objects using factory mode-->
    <!-- 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("Student object creation");
    }

    @Override
    public void eat() {
        System.out.println("How students eat");
    }

    @Override
    public void run() {
        System.out.println("How to run");
    }
}
package com.spring3;

public class Teacher implements People {

    public Teacher() {
        System.out.println("Teacher object creation");
    }

    @Override
    public void eat() {
        System.out.println("How the teacher eats");
    }

    @Override
    public void run() {
        System.out.println("The way the teacher runs");
    }
}
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();
    }
}

Operation result:

Topics: Spring xml Attribute encoding