SSM foundation Spring IOC container

IOC

What is IOC? IOC(Inverse of Control) is a design principle in object-oriented programming, which can be used to reduce the coupling between computer codes. Reverse the creation power of objects to the Spring framework!

In java, if a class wants to use the method of another class, it must create the object of this class in this class. Then the following situations may occur, such as B object in class A, C object in class B and a object in class C. if there is a problem with a class, it may lead to a problem in this framework. Spring gives the right to create objects to IOC. Has ABC three objects been created in IOC? Then our other classes only need to call collections, which greatly solves the problem of program coupling.

What problems does IOC solve? Using IOC can solve the problem of high program coupling.

Underlying principle of IOC container

The implementation of IOC depends on the following three technologies

  1. dom4j parses xml documents;
  2. Factory mode;
  3. Create objects in reflective design mode

It is an open-source XML based API with high performance. By computer.

Factory Method: define an interface for creating objects and let subclasses decide which class to instantiate. Factory Method delays the instantiation of a class to its subclasses.

Create objects in reflective design mode: class Newinstance() or constructor newInstance() 

Spring implements IOC

public class Demo {
    public void hello() {
        System.out.println("hello world");
    }
}

1.xml configuration file, configuration creation object

<bean id="demo" class="com.qcby.Demo" />

2. Create factory class

public class DemoFactory {
    //Use dom4j to get the value corresponding to name
   public static Demo getDemo() throws Exception {
       //Use dom4j to get the value corresponding to name
       String value="class route";//The path of the demo in the project
       //Create objects by reflection
       Class clazz = Class.forName(value);
       //Go back and create the demo object
       return (Demo) clazz.newInstance();
   }
}

Through the above two steps, we can basically get the object we created.

IOC (Interface)

Create maven project, and create com.com in scr\main\java qcby. Demo class

public class Demo {
    public void hello() {
        System.out.println("hello world");
    }
}

Configure springconfig. In scr\main\resources 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">
    <!--IOC Administration bean-->
    <!--id:Unique identifier of the class class:The full pathname of the class-->
    <bean id="demo" class="com.qcby.Demo" ></bean>
</beans>

The idea of IOC is based on the IOC container. The bottom layer of IOC is the object factory.

Spring provides two ways to implement IOC containers:

  • BeanFactroy: IOC container is the internal interface of Spring and is not provided for developers
public class DemoTest {
        //Traditional writing
        @Test
        public void run(){
            Demo demo = new Demo();
            demo.hello();
        }
        //spring writing
        @Test
        public void run1(){
            //Create a spring factory and load the configuration file
            BeanFactory ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            //Get bean object
            Demo demo = (Demo) ac.getBean("demo");
            demo.hello();
        }
}

[note] BeanFactroy: objects are not created when the configuration file is loaded, but only when the object is used

  • ApplicationContext: the sub interface of beanfactory interface, which provides more and more powerful functions and is generally used by developers

public class DemoTest {
        //Traditional writing
        @Test
        public void run(){
            Demo demo = new Demo();
            demo.hello();
        }
        //spring writing
        @Test
        public void run1(){
            //Create a spring factory and load the configuration file
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            //Get bean object
            Demo demo = (Demo) ac.getBean("demo");
            demo.hello();
        }
}

[note] the object will be created when loading the configuration file

Keywords: Java Spring Container SSM

Added by veluit06 on Sat, 19 Feb 2022 19:25:17 +0200