[1] Spring overview, IoC theory derivation

Spring

1. Spring overview

1.1 Introduction

Spring: Spring—>Bring spring to the software industry

In 2002, Rod Jahnson first launched the Spring framework prototype interface21 framework.

On March 24, 2004, the Spring framework, based on the interface21 framework, was redesigned and released the official version 1.0.

It's hard to imagine Rod Johnson's degree. He is a PhD from the University of Sydney. However, his major is not computer, but music.

Spring concept: Make existing technology more practical. It is a hodgepodge in itself, integrating existing framework technology

Official website: http://spring.io/

Official download address: https://repo.spring.io/libs-release-local/org/springframework/spring/

GitHub : https://github.com/spring-projects

I use maven here, just go to the maven warehouse to copy the dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.0</version>
    </dependency>
</dependencies>

1.2, advantages

1.Spring is an open source and free framework, container.

2.Spring is a lightweight framework, non-intrusive.

3. Inversion of control IoC, face-to-face Aop

4. Support for things, support for frameworks

One sentence summary:

Spring is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container (framework).

1.3 Composition

Insert picture description here

The Spring framework is a layered architecture consisting of 7 well-defined modules. The Spring module is built on the core container, which defines the way to create, configure, and manage beans.

Insert picture description here

Each module (or component) that composes the Spring framework can exist alone or be implemented in combination with one or more other modules. The functions of each module are as follows:

  • Core container : The core container provides the basic functions of the Spring framework. The main component of the core container is BeanFactory, which is an implementation of the factory pattern. BeanFactory uses the Inversion of Control (IOC) model to separate the configuration and dependency specifications of the application from the actual application code.
  • Spring context : Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, verification and scheduling functions.
  • Spring AOP : Through the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming functions into the Spring framework. Therefore, you can easily make the Spring framework manage any object that supports AOP. The Spring AOP module provides transaction management services for objects in Spring-based applications. By using Spring AOP, you can integrate declarative transaction management into your application without relying on components.
  • Spring DAO : The JDBC DAO abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exceptions follow the general DAO exception hierarchy.
  • Spring ORM : Spring framework inserts several ORM frameworks, thereby providing ORM object relational tools, including JDO, Hibernate and iBatis SQL Map. All of these follow Spring's general transaction and DAO exception hierarchy.
  • Spring Web module : The Web context module is built on top of the application context module to provide context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The web module also simplifies the work of handling multipart requests and binding request parameters to domain objects.
  • Spring MVC framework : MVC framework is a full-featured MVC implementation for building Web applications. Through the strategy interface, the MVC framework becomes highly configurable. MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, iText, and POI.

1.4, extension

Spring Boot与Spring Cloud

  • Spring Boot is a set of rapid configuration scaffolding for Spring, which can quickly develop a single microservice based on Spring Boot;
  • Spring Cloud is based on Spring Boot;
  • Spring Boot focuses on a single microservice individual that is fast and easy to integrate, and Spring Cloud focuses on the global service governance framework;
  • Spring Boot uses the concept of constraint over configuration. Many integration solutions have been selected for you. You can configure it without configuration. A large part of Spring Cloud is implemented based on Spring Boot. Spring Boot can be used and developed independently of Spring Cloud. Project, but Spring Cloud is inseparable from Spring Boot, which is a dependency relationship.
  • SpringBoot plays a connecting role in SpringClound. If you want to learn SpringCloud, you must learn SpringBoot.

Insert picture description here

2. IOC theory derivation

IOC basics

Create a blank maven project

2.1, analysis and realization

Write a piece of code in the original way:

1. Write a UserDao interface first

public interface UserDao {
    
    
   public void getUser();
}

2. Write Dao's implementation class again

public class UserDaoImpl implements UserDao {
    
    
   @Override
   public void getUser() {
    
    
       System.out.println("获取用户数据");
  }
}

3. Then write the interface of UserService

public interface UserService {
    
    
   public void getUser();
}

4. Finally, write the implementation class of Service

public class UserServiceImpl implements UserService {
    
    
   private UserDao userDao = new UserDaoImpl();

   @Override
   public void getUser() {
    
    
       userDao.getUser();
  }
}

5. Test it

@Test
public void test(){
    
    
   UserService service = new UserServiceImpl();
   service.getUser();
}

This is our original way. At the beginning, everyone wrote in the same way, right. Let's modify it now.

Add a Userdao implementation class.

public class UserDaoMySqlImpl implements UserDao {
    
    
   @Override
   public void getUser() {
    
    
       System.out.println("MySql获取用户数据");
  }
}

Next, if we want to use MySql, we need to modify the corresponding implementation in the service implementation class

public class UserServiceImpl implements UserService {
    
    
   private UserDao userDao = new UserDaoMySqlImpl();

   @Override
   public void getUser() {
    
    
       userDao.getUser();
  }
}

In the hypothesis, we will add another Userdao implementation class.

public class UserDaoOracleImpl implements UserDao {
    
    
   @Override
   public void getUser() {
    
    
       System.out.println("Oracle获取用户数据");
  }
}

Then we want to use Oracle, and we need to modify the corresponding implementation in the service implementation class. Assuming that our needs are very large, this method is not applicable at all, and even anti-human, right, every change requires a lot of modification Code. The coupling of this design is too high, and it affects the whole body.

So how do we solve it?

We can not implement it where we need to use it, but leave an interface, use set, we go to the code to modify it.

public class UserServiceImpl implements UserService {
    
    
   private UserDao userDao;
// 利用set实现
   public void setUserDao(UserDao userDao) {
    
    
       this.userDao = userDao;
  }

   @Override
   public void getUser() {
    
    
       userDao.getUser();
  }
}

Now go to our test class to test;

@Test
public void test(){
    
    
   UserServiceImpl service = new UserServiceImpl();
   service.setUserDao( new UserDaoMySqlImpl() );
   service.getUser();
   //那我们现在又想用Oracle去实现呢
   service.setUserDao( new UserDaoOracleImpl() );
   service.getUser();
}

Have you noticed the difference? Maybe many people say there is no difference. But classmates, they have undergone a fundamental change, and many places are different. Think about it carefully, everything used to be controlled and created by programs before , And now we control the creation of the object by ourselves, giving the initiative to the caller. The program does not have to worry about how to create it or how to implement it. It is only responsible for providing an interface.

This kind of thinking essentially solves the problem. We programmers no longer manage the creation of objects, and pay more attention to the realization of the business. The coupling is greatly reduced. This is the prototype of IOC!

2.2 The essence of IOC

IoC (Inversion of Control) is a design idea. DI (Dependency Injection) is a way to implement IoC. Some people think that DI is just another term for IoC. In a program without IoC, we use object-oriented programming. The creation of objects and the dependencies between objects are completely hard-coded in the program. The creation of objects is controlled by the program itself. After the inversion of control, the creation of objects is transferred to a third party, personal It is believed that the so-called inversion of control is: the way of obtaining dependent objects is reversed.

Insert picture description here

IoC is the core content of the Spring framework. IoC is perfectly implemented in a variety of ways. You can use XML configuration or annotations. The new version of Spring can also implement IoC with zero configuration.

The Spring container first reads the configuration file when it is initialized, creates and organizes objects according to the configuration file or metadata and stores them in the container, and then takes out the required objects from the Ioc container when the program is used.

Insert picture description here

When configuring the Bean in XML mode, the definition information of the Bean is separated from the implementation, and the two can be integrated in the way of annotations. The definition information of the Bean is directly defined in the implementation class in the form of annotations, thus achieving zero The purpose of the configuration.

Inversion of control is a way to produce or obtain specific objects through description (XML or annotation) and through a third party. It is the IoC container that implements the inversion of control in Spring, and its implementation method is Dependency Injection (DI).

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/109587288