What is Spring?
Spring is a lightweight, non-intrusive framework, which advocates the “least intrusive” way to manage the code in the application, the purpose is to solve the problem of coupling between the business logic layer and other layers of enterprise-level application development, the most fundamental The mission is to solve the complexity of enterprise-level application development, that is, to simplify Java development.
There are two core concepts in the Spring framework. These two core concepts are the cornerstones of Spring’s success:
- Inversion of control (
Inversion of Control, IoC) or dependency injection (
dependency injection, DI)
- Aspect-oriented programming (
aspect-oriented programming, AOP)
The design goals, design concepts and core of the Spring framework:
- Design goal : to provide developers with a one-stop lightweight application development platform.
- Design concept : In the development of JavaEE, support pojo and JavaBean development methods, make application interface-oriented development, and fully support object-oriented design methods; realize the management of object coupling relationship through IoC container, and realize dependency inversion, and the dependency between objects The relationship is handed over to the IoC container to achieve decoupling.
- The core of the framework : IoC container and AOP module. Manage pojo objects and their coupling relationships through the IoC container; enhance services in a dynamic and non-invasive way through AOP. IoC allows cooperating components to remain loosely coupled, while AOP programming allows functions that are spread across all layers of the application to be separated to form reusable functional components.
Spring has about 20 modules in total, composed of more than 1,300 different files, and these components are integrated in the following modules:
☕️ Core Container
- Core module: Provides the basic core tool classes of the framework, including the most basic implementation of dependency injection IoC and DI, and other components must use this module class.
- Beans module : Provides the BeanFactory interface, which is a classic implementation of the factory pattern. This module is responsible for initializing various Beans and calling their life cycle methods. Spring refers to the management objects in the IoC container as Beans.
- Context module: Spring’s context, which provides a runtime environment for Spring and saves the state of objects. This module is based on the Core and Beans sub-modules. The ApplicationContext interface provided is an extension of the BeanFactory of the Beans module and provides a framework for object access.
- SpEL module : Provides a powerful expression language to support runtime query and manipulate object graphs.
☕️ Data Access/Integeration
- JDBC module : Provides a JDBC package, which greatly reduces the coding of database operations during the development process.
- ORM module :
Object-Relational MappingProvides an integration layer for popular object-relational mapping ( ) APIs, including JPA and Hibernate.
- OXM module : Provides an abstraction layer implementation that supports object and XML mapping, such as JAXB, Castor, JiBX and XStream.
- JMS module : Refers to the Java messaging service, including functions for producing and using messages.
- Transactions module : Transaction module, which supports programming and declarative transaction management for implementing special interfaces and all POJO classes.
- Web module : Provides basic Web development integration functions, such as multi-file upload function, using Servlet listener to initialize an IOC container and Web application context.
- WebMvc module : Contains
REST Web Servicesimplementation for Web applications , provides a clear separation between domain model code and Web forms, and integrates with all other functions of the Spring Framework.
- WebSocket module : Provides the implementation of WebSocket and SocketJS.
- WebFlux module : Spring5 new module is a new set of
Reactive Webstack technology, which is completely non-blocking.
☕️ Other modules
- AOP module : Provides an aspect-oriented programming implementation, allowing method interceptors and entry points to be defined, and code is separated according to functions to reduce coupling.
- Aspects module : Provides integration with AspectJ, which is a powerful and mature aspect-oriented programming framework.
- Instrumentation module : Provides the support of class tools and the realization of class loaders, which can be used in specific application servers.
- Messaging module : Provides support for messaging architecture and protocols.
- Test module : Supports unit testing and integration testing of Spring components using JUnit or TestNG.
Advantages and disadvantages of Spring
⭐️ Advantages of Spring framework:
- Facilitate decoupling and simplify developmentThrough the lOC container provided by Spring, the creation and dependencies of objects can be controlled by Spring, avoiding over-coupling caused by hard coding, and there is no need to write code for very low-level requirements such as singleton pattern classes and property file analysis. , You can focus more on the upper application.
- AOP programming supportSpring provides aspect-oriented programming. Many functions that are not easy to implement with traditional OOP can be easily implemented through AOP, such as program permission interception and operation monitoring.
- Support for declarative transactionsOnly need to configure to complete the management of the transaction, without manual programming.
- Convenient program testingSpring supports Junit4/Junit5, and you can easily test Spring programs through annotations.
- Convenient integration of various excellent frameworksSpring provides direct support for various excellent frameworks (such as Struts, Hibernate, MyBatis, etc.).
- Reduce the difficulty of using JavaEE APISpring provides encapsulation for some very difficult APIs (JDBC, JavaMail, remote calls, etc.) in JavaEE development to greatly reduce the difficulty of applying these APIs.
⭐️ Disadvantages of Spring framework:
- Spring relies on reflection, which affects program execution performance.
Understanding of IoC & DI
✏️ What is IoC?
IoC-Inversion of Control, or “inversion of control” , is not a technology, but a design idea .
The so-called “control” refers to the control of the object, that is, the life cycle operations of the creation, initialization, and destruction of the object; the so-called “inversion of control” refers to the reversal of the control of the object, that is, the object Operations such as creation, initialization, destruction, etc. are managed by the IoC container, instead of being directly controlled inside your object.
In traditional Java SE programming, our program is the direct controller of the object, responsible for the creation, initialization, and destruction of the object; while in Spring, the creation, initialization, and destruction of the object are taken care of by the IoC container, and our program cannot To determine any stage of the object’s life cycle, at best, some minor actions are made with the help of Spring’s extension mechanism.
- Create an object: originally it was an object new, now it is created by the IoC container;
- Initialize the object: For example, A depends on B, which was originally assigned through the constructor or setter method, but now the dependent object is searched and injected by the IoC container;
- Destroying objects: Originally assigning objects to null directly or doing some destruction operations, now the IoC container management life cycle is responsible for destruction.
In other words, we have changed from the direct controller of the object to the passive recipient of the IoC container, and the control over the life cycle of the object has been reversed.
✏️ What can IoC do?
IoC is not a technology, but an idea, an important object-oriented programming law, which can guide us to design loosely coupled and better programs. The control of creating and finding dependent objects is handed over to the container, and the container injects the composite object, so the object and the object are loosely coupled, which is conducive to functional reuse, and more importantly, makes the entire system structure of the program very flexible .
In fact, the biggest change IoC brings to programming is not from the code, but from the ideology, the “master-slave transposition” change has occurred. The application was originally the boss, and all resources needed to be obtained were proactive, but in the IoC/DI idea, the application became passive, passively waiting for the IoC container to create and inject the resources it needs.
✏️ What is DI?
DI-Dependency Injection, or “dependency injection” , is an important implementation of IoC. From IoC to DI, it is from theory to practice.
The creation of an object often involves the creation of other objects. For example, a member variable of an object A holds a reference to another object B. This is a dependency, and A depends on B. Since the IoC mechanism is responsible for the creation of objects, the IoC container must be responsible for this dependency, and the method of responsibility is DI-dependency injection.
By writing the dependencies into the configuration file, and then when creating objects with dependencies, the IoC container is responsible for injecting the dependent objects.
For example, when A is created, it is checked that there is a dependency, and the IoC container creates the object B that A depends on After injecting into A (assembly, realized by reflection mechanism), and then returning A to the object requester to complete the work.
To understand dependency injection, the focus is on the two concepts of “dependency” and “injection”. What is dependency? The external data and resources required for the operation of the object are dependencies. Without these things, the object cannot complete the business processing and must be obtained to run.
What is injection?
The word injection is really vivid, just like an injection, from the outside to the inside, the container loads external files, URLs, configurations, and objects, and then injects these data and objects into the objects on demand.
IoC and DI are descriptions of the same concept from different perspectives, but they are actually different. IoC emphasizes the reversal of the control of the container and the object, while DI emphasizes that the dependency of the object is injected by the container. In most cases, it is not wrong to say that the two are the same.
But in a broad sense, IoC is a software development model, which means that it can be implemented in other ways, and DI is just one of them. Spring chose DI to make DI deeply rooted in Java development.