Basic summary of spring IOC

Posted by Graxeon on Fri, 28 Jan 2022 16:25:03 +0100

 

Article catalogue

catalogue

Article catalogue

1, What is Spring

2, Advantages of Spring framework

 

3, Detailed explanation of spring IOC mechanism

 

IOC role summary

Detailed explanation of spring IOC mechanism

1, First build maven project and add dependencies

2, Create persistence layer and implementation class

3, Create business layer and implementation classes

4, Create an xml file with any name in the resources folder

 

5, Test class

 

summary

 

1, What is Spring

Spring is a layered Java Se / EE (WEB) full stack (one-stop) lightweight open source framework, with IOC (Inverse Of Control) and AOP (Aspect Oriented Programming) as the kernel.

One stop shop: Spring can provide development support in any Java EE direction.

Lightweight: the current framework has low requirements for hardware, which is easy to start and use

Open source: free open source

Seamless link: integrate the Java EE development technology we currently use on the market (melting furnace, melting iron blocks of various shapes and materials)

Corresponding technologies are provided in the three-tier java architecture:

Presentation layer (web layer): Spring MVC (framework)

Business layer (service layer): Bean Management (IOC container)

Persistence layer (dao layer): JdbcTemplate template object and other excellent persistence layer technologies that provide ORM module integration

2, Advantages of Spring framework

Facilitate decoupling and simplify development: Spring is a big factory that can manage the creation and dependency maintenance of all objects, which is managed by Spring

AOP programming support: it can easily realize the permission interception, logging and operation monitoring of the program

Declarative transaction support: complete transaction management through configuration without manual programming

Convenient program testing: for Junit support, you can easily test Spring programs through annotations

Integrate external excellent technologies: Spring provides direct support for various excellent frameworks (Hibernate, Mybatis, Quartz, etc.)

Encapsulation of javaEE Technology: Spring encapsulates complex and difficult APIs (JavaEmail, RMI, etc.) in the development of javaEE, which reduces the difficulty of using these APIs

3, Detailed explanation of spring IOC mechanism

Spring IOC: books are generally translated into control inversion, control inversion, etc

Control: Spring provides a container for controlling objects in java development

Meaning of inversion: the program itself does not create an object, but becomes a passive receiving object

In 1996, Michael Mattson first proposed the concept of IOC in an article about exploring object-oriented framework. In short, it is to decompose the complex system into cooperative objects. After these object classes are encapsulated, the internal implementation is transparent to the outside, which reduces the complexity of solving problems and can be reused and extended flexibly.

 

Due to the introduction of the intermediate IOC container, there is no direct coupling relationship between the four objects A, B, C and D. the rotation of the four gears all depends on the intermediate IOC container, so many people regard the IOC container as an adhesive.

IOC role summary

IOC is essentially a big project, a big container. The main function is to create and manage the dependency of objects, reduce the coupling of computer programs (remove the dependency in our code), and improve the scalability and maintainability of programs.

Detailed explanation of spring IOC mechanism

1, First build maven project and add dependencies

     <properties>
        <spring.version>5.2.5.RELEASE</spring.version>
    </properties>
    <!-- Import the context coordinates of spring. The context depends on core, beans, expression AOP -- >
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

2, Create persistence layer and implementation class

//UserMapper interface
package com.ujiuye.mapper;

public interface UserMapper {
    void getUser();
}

//UserMapperImpl implementation class

public class UserMapperImpl implements UserMapper{
    public void getUser(){
        System.out.println("Get user data by default");
    }
}

3, Create business layer and implementation classes

 

//UserService interface
import com.ujiuye.mapper.UserMapper;

public interface UserService {
    void getUser();
    void setUserMapper(UserMapper userMapper);
}

//UserServiceImpl implementation class

public class UserServiceImpl implements UserService{
    private UserMapper userMapper;
    //Dynamic value injection using set
    @Override
    public void getUser() {
        userMapper.getUser();
    }
    @Override
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper=userMapper;
    }
}

4, Create an xml file with any name in the resources folder

 

//The specification is named ApplicationContext 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    use Spring To create objects, in Spring These become Bean
        id Equivalent to variable name
        class Relative and new Object of
        property This is equivalent to setting a value for an attribute in an object
-->
    <bean name="userMapperImpl" class="com.ujiuye.mapper.UserMysqlMapperImpl"/>
    <bean name="userServiceImpl" class="com.ujiuye.service.UserServiceImpl"/>
     
</beans>

5, Test class

public class TestSpring {
    @Test
    public void test1(){
        //Get the context object of Spring: get the container of Spring
         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //Our objects are now managed in Spring. We need to use them now. Just take them out directly
        Hello hello =(Hello) context.getBean("hello");
        UserServiceImpl userService =(UserServiceImpl) context.getBean("ServiceImpl");
        userService.getUser();

    }

 

summary

Tip: here is a summary of the article:
For example, the above is what we want to talk about today. This paper only briefly introduces the use of pandas, which provides a large number of functions and methods that enable us to process data quickly and conveniently.

Topics: Java Maven Mybatis Spring Spring Boot