Basic Overview
Spring is a layered, full-stack, lightweight open-source framework. With IoC and AOP as its core, it provides the presentation layer Spring MVC and business layer management, along with many enterprise application technologies. It has become the most widely used Java EE enterprise application open-source framework.
Spring’s official address:
https://spring.io/
Development History
In 1997, IBM proposed the EJB concept. In 1998, SUN released development standard EJB 1.0. In 1999, EJB 1.1 was released. In 2001, EJB 2.0 was released. In 2003, EJB 2.1 was released. In 2006, EJB 3.0 was released.
Origin of Spring (2002)
The Spring framework was initially developed by Rod Johnson, with the primary goal of solving complex problems in Java enterprise applications at the time, especially the shortcomings and overly complex features of EJB (Enterprise JavaBeans). Rod Johnson published “Expert One-on-One J2EE Design and Development” in 2002, which introduced Spring’s design philosophy and core concepts. This book became the foundation of the Spring framework.
Birth of Spring Framework (2003)
The first version of the Spring framework was released in 2003. Spring’s core initial goal was to simplify enterprise application development, particularly to replace EJB. Spring’s main features include:
- Inversion of Control (IoC): Based on the Dependency Injection (DI) concept, implementing object creation and management through configuration files or annotations.
- Aspect-Oriented Programming (AOP): Used to decouple cross-cutting concerns (such as transaction management, logging, etc.).
- Data Access: Spring provides simplified support for JDBC and ORM frameworks (such as Hibernate), reducing JDBC programming complexity.
Spring 2.0 (2006)
Spring 2.0 was released in 2006 with many new features and improvements, making it more mature and powerful:
- Annotation-driven configuration: Spring 2.0 introduced annotation support, allowing developers to configure Beans through annotations rather than only through XML configuration files.
- AOP enhancements: Spring’s AOP framework received significant improvements, supporting more cross-cutting concerns.
- Spring MVC improvements: Spring MVC, as part of the Spring framework, continued to receive feature expansions and performance optimizations.
Spring 3.0 (2009)
Spring 3.0 was released in 2009, an important version that introduced many modern features:
- Java 5 and Java 6 support: Spring 3.0 requires a minimum Java 5 version and supports Java 5 features (such as generics, enums, annotations, etc.).
- RESTful Web services: Spring 3.0 added support for RESTful Web services, promoting changes in Web development approaches.
- Spring Expression Language (SpEL): Introduced a powerful expression language allowing dynamic expressions in configuration.
- Improved Spring MVC: Strengthened Spring MVC’s support for modern Web applications, improving annotation-driven and formatting features.
Spring 4.0 (2013)
Spring 4.0 was released in 2013 with the following major updates:
- Java 8 support: Spring 4.0 added support for Java 8 Lambda expressions and new features.
- WebSocket support: Spring 4.0 added WebSocket support, making it easier to develop real-time Web applications.
- Better REST support: Spring 4.0 further enhanced support for RESTful Web services, especially in Content Negotiation and response body formatting.
- Groovy support: Spring 4.0 introduced support for the Groovy programming language, allowing developers to choose Groovy as a scripting language for Spring.
Spring 5.0 (2017)
Spring 5.0 was released in 2017, an important version involving refactoring and new features in multiple areas:
- Spring WebFlux: Spring 5.0 introduced the WebFlux module supporting reactive programming. WebFlux is based on Project Reactor, allowing developers to build high-performance, non-blocking Web applications using reactive programming models.
- Java 8 and above support: Spring 5.0 fully supports Java 8 and above features (such as Stream API, Optional, Lambda, etc.).
- Internal framework refactoring: Spring 5.0 underwent extensive refactoring, simplifying internal implementations while improving performance and maintainability.
- Kotlin support: Spring 5.0 began officially supporting Kotlin as an option for developing modern Spring applications.
Spring Boot (2013)
Spring Boot was released in 2013 by the Spring team to simplify Spring application configuration and deployment. Its goal is to allow developers to quickly start and run Spring applications without extensive configuration. Spring Boot’s main features include:
- Auto-configuration: Spring Boot automatically provides default configuration for applications without manual configuration.
- Embedded servers: Spring Boot allows developers to package applications as standalone executable JAR files with embedded Web servers (such as Tomcat, Jetty, etc.), without relying on external servers.
- Spring Initializr: A Web tool helping developers quickly generate Spring Boot project skeletons.
Spring Cloud (2014)
Spring Cloud is a solution for distributed systems, built on Spring Boot and the Spring framework. It helps developers quickly implement microservices architecture and provides common distributed system patterns such as:
- Configuration management: Spring Cloud Config provides centralized configuration management.
- Service discovery: Through Spring Cloud Netflix Eureka, services can discover each other automatically.
- Load balancing: Spring Cloud provides client-side load balancing functionality (through Ribbon).
- Circuit breaker pattern: Using Hystrix to implement fault tolerance management for services.
Spring 6.0 (2023)
Spring 6.0 is the latest version of the Spring framework, released in 2023, with many modern updates:
- Full Java 17 and above support: Spring 6.0 no longer supports earlier Java versions, requiring a minimum of Java 17.
- Spring Native: Spring 6.0 further improved support for native compilation, especially GraalVM compatibility, allowing Spring applications to compile to native images for faster startup and lower memory usage.
- Modernized dependency management: Updated all dependencies and tools, removed deprecated APIs, and simplified the framework.
- More cloud platform support: Spring 6.0 enhanced cloud platform support (such as Kubernetes), making Spring more suitable for modern cloud-native applications.
Spring Advantages
Spring is a comprehensive framework with strong ideological foundations.
- Convenient decoupling, simplified development: Through Spring’s IoC container, you can hand over object dependency relationships to Spring for control, avoiding the excessive coupling caused by hard-coding. Users no longer need to write code for底层 needs like singleton pattern classes and property file parsing, allowing them to focus on upper-layer code.
- AOP programming support: Through Spring’s AOP functionality, convenient aspect-oriented programming is available. Many functions that are difficult to implement with traditional OOP can be easily handled with AOP.
- Declarative transaction support: Transactional allows us to break free from tedious transaction management, flexibly managing transactions declaratively to improve development efficiency and quality.
- Convenient testing: You can perform almost all testing work using non-container-dependent programming methods. Testing is no longer an expensive operation but something readily available.
- Convenient integration of various excellent frameworks: Spring can reduce the difficulty of using various frameworks, providing direct support for many excellent frameworks.
- Reduced difficulty of JavaEE API usage: Spring encapsulates JavaEE APIs (such as JDBC, JavaMail, remote calls), greatly reducing the difficulty of using these APIs.
- Source code is a classic Java learning example: Spring’s source code is exquisitely designed, clearly structured, and uniquely crafted. It reflects the master’s flexible use of Java design patterns and profound Java technical expertise. Its source code is undoubtedly the best practical example of Java technology.
Core Structure
Spring is a very clearly layered lightweight framework with very clear dependency relationships and responsibility positioning. It mainly includes several modules: data processing module, Web module, AOP (Aspect Oriented Programming), Core Container, Test, and other modules. Relying on these modules, Spring has completed a zero-invasive lightweight framework with existing solutions.
- Spring Core Container (Core Container): The container is the most core part of the Spring framework. It manages the creation, configuration, and management of Beans in Spring applications. In this module, there is the Spring Bean factory, which provides DI functionality for Spring. Based on the Bean factory, we also find various Spring application context implementations. All Spring modules are built on top of the container.
- Aspect-Oriented Programming (AOP): Spring provides rich support for aspect-oriented programming. This module is the foundation for developing aspects in Spring applications. Like DI, AOP can help application objects decouple.
- Data Access and Integration: Spring’s JDBC and DAO modules encapsulate a large amount of code, making database code concise and allowing you to focus more on your business. It also avoids problems caused by database resource release failures. Additionally, Spring AOP provides transaction management services for data access. Spring also integrates ORMs such as MyBatis. This module consists of JDBC, Transactional, ORM, OXM, and JMS modules.
- Web: This module provides the Spring MVC framework. Spring provides a testing module dedicated to Spring application testing. Through this module, Spring provides a series of Mock object implementations for writing unit tests with Servlets, JNDI, etc.
Core Concepts
IoC and AOP were not proposed by Spring. They existed before the Spring framework appeared, but they were more theoretical. Spring did an excellent job implementing them at the technical level.
IoC
IoC full name: Inversion of Control. Note that it is a technical thought, not a technical implementation. It is mainly used for object creation and management issues in Java development. Traditional development approach: For example, if class A depends on class B, class A often creates a new class B object.
Under the IoC thought: We don’t need to new the object ourselves. Instead, the IoC container (Spring framework) helps us instantiate and manage objects. When we need an object, we retrieve it from the IoC.
What Problems Does IoC Solve
IoC solves the coupling problem between objects.
Difference Between IoC and DI
DI: Dependency Injection IoC and DI actually describe the same thing, but from different angles.
AOP
AOP: Aspect-Oriented Programming. AOP is the continuation of OOP. OOP’s three characteristics: inheritance, encapsulation, polymorphism. OOP is a vertical inheritance system.
Problems That Arise
OOP programming ideas can solve most code duplication problems, but there are some situations that cannot be handled. For example, when the same position in multiple methods of a top-level parent class Animal has duplicate code, OOP has difficulty solving this.
Cross-Cutting Logic Code
What is cross-cutting logic code:
- Cross-cutting code duplication problem
- Cross-cutting logic code mixed with business code causes code bloat and difficult maintenance
AOP enters the picture, taking a different path to propose a horizontal abstraction mechanism, separating cross-cutting logic and business logic code. Code splitting is easy, but how to silently apply cross-cutting logic code to the original business logic without changing the original business logic to achieve the same effect.
What Problems Does AOP Solve
Without changing the original business, enhance cross-cutting logic code, fundamentally decoupling and avoiding cross-cutting logic code duplication.