Apparently, the black cat still hung up. Let’s just do the basics.
First, About Spring
Spring is a lightweight open source framework that is a one-stop shop for layered Java SE/EE applications.
The main advantages of Spring include:
- It is convenient to decouple and simplify development. Through the IoC container provided by Spring, we can control the dependencies between objects by Spring, avoiding the high degree of program coupling caused by hard coding.
- AOP programming support, with AOP functionality provided by Spring, facilitates section-oriented programming.
- Support for declarative transactions. In Spring, we can get rid of the tedious transaction management code and manage transactions flexibly in a declarative way, improving development efficiency and quality.
- Easy to test programs, you can use non-container-dependent programming to do almost all of your testing.
- It’s easy to integrate excellent frameworks, and Spring provides direct support for excellent frameworks.
The core of Spring is IOC and AOP.
IOC
IOC (Inversion Of Controll) is a design idea that gives the Spring framework control over manually created objects in a program. IOC container is the carrier used by Spring to implement IOC. IOC container is actually a Map(key, value), which stores various objects.
AOP
AOP (aspect-oriented Programming, section-oriented Programming) can encapsulate the logic or responsibility (such as transaction processing, log management, permission control, etc.) that has nothing to do with business but is called jointly by business modules, so as to reduce the repeated code of the system and reduce the degree of coupling between modules. And is conducive to future scalability and maintainability. After using AOP, we can abstract out some general functions and use them directly where needed, which can greatly simplify the amount of code and improve the scalability of the system.
Spring AOP/AspectJ AOP
Spring AOP is runtime enhancement, while AspectJ is compile-time enhancement.
Spring AOP is based on Proxying, while AspectJ is based on Bytecode Manipulation.
AspectJ is more powerful than Spring AOP, but Spring AOP is relatively simple. If there are fewer sections, there is little difference in performance between the two. However, when there are too many facets, AspectJ is the best choice, which is much faster than SpringAOP.
Spring Architecture
As shown in the figure below, the entire Spring framework can be divided into five main modules based on their functions. These five modules provide almost everything enterprise applications need, from the persistence layer to the business layer to the presentation layer, which is why Spring is called a one-stop framework.
1. Core Container
Spring’s core module implements IoC by separating classes and dependencies between classes from code and describing them in a configuration way. The IoC container is responsible for class creation, management, fetching, and so on. The BeanFactory interface is the core interface of the Spring framework and implements many of the core functions of the container.
Context module is built on the core module and extends the functions of BeanFactory, including internationalization, resource loading, mail service, task scheduling and many other functions. ApplicationContext is the core interface of the Context module.
Expression Language is an extension of the Unified Expression Language (EL). It allows you to set and get object properties, call object methods, manipulate arrays, collections, and more. It makes it easy to interact with the Spring IoC container through expressions.
2. AOP module
The Spring AOP module provides implementations that meet the AOP Alliance specification and also integrates AspectJ, an AOP language-level framework. Coupling can be reduced through AOP.
3. Data Access/Integration module
This module includes JDBC, ORM, OXM, JMS and transaction management:
- Transaction module: This module is used for Spring transaction management, as long as the Spring managed objects can get the benefits of Spring transaction management, no need to conduct transaction control in code, and support programmatic and declarative transaction management.
- JDBC module: Provides a sample JBDC template that eliminates traditional verbose JDBC coding and the necessary transaction control, and benefits from Spring transaction management.
- ORM modules: Provide seamless integration with popular “object-relational” mapping frameworks, including Hibernate, JPA, MyBatis, etc. And you can use Spring transaction management without additional control over transactions.
- OXM module: Provides an Object/XML mapping implementation to map Java objects to XML data, or XML data to Java objects. Object/XML mapping implementations include JAXB, Castor, XMLBeans, and XStream.
- JMS module: Used for Java Messaging Service (JMS). It provides a set of Message Producer and Message consumer templates for easier use of JMS. JMS is used to send messages for asynchronous communication between two applications or in a distributed system.
4. Web module
This module builds on the ApplicationContext module and provides Web application functions such as file uploads and FreeMarker. Spring can integrate MVC frameworks such as Struts2. In addition, Spring provides its own MVC framework, Spring MVC.
5. Test module
Spring can do almost all of its testing in a non-container-dependent programming style, supporting testing frameworks such as JUnit and TestNG.
The last
Have you gained a deeper understanding of Spring after reading this post?
For those who need more interview resources, you can pay attention to my public account [Java Technology zhai].
If there are friends who see this article and happen to have supplementary content to share with Spring, welcome to leave a message and exchange.