01 Understanding the Spring Family Ecosystem Correctly

01 Understanding the Spring Family Ecosystem Correctly #

Since its design and implementation by Rod Johnson in 2003, the Spring framework has gone through several major versions of development and evolution, and has formed a large family-style technical ecosystem. Currently, Spring is the most popular development framework in the Java EE field, and is widely used in enterprises worldwide.

Today, I will take you through the entire technical system of the Spring family and various fully functional development frameworks in one lesson. Let’s start by taking a look at the panoramic view of the Spring family’s technical ecosystem.

Panorama of the Spring Family’s Technical Ecosystem #

We can visit the official website of Spring (https://spring.io/) to get a macro understanding of this framework. On Spring’s homepage, the following image is displayed:

Drawing 0.png

Technical system of the Spring family (from Spring official website)

From the image, we can see that the seven core technical systems of the Spring framework are listed here, namely Microservices Architecture, Reactive Programming, Cloud Native, Web Applications, Serverless Architecture, Event-driven, and Batch Processing.

Of course, these technical systems are independent, but they also have some intersections. For example, Microservices Architecture is often combined with cloud-native technologies based on Spring Cloud, and the construction process of Microservices Architecture also relies on the ability to provide RESTful-style web applications.

On the other hand, besides their specific technical features, these technical systems also have their own application scenarios. For example, if we want to implement lightweight batch processing tasks such as daily reports without introducing the huge offline processing platform like Hadoop, using the Spring Batch-based batch processing framework is a good choice. For another example, if we want to integrate with various mainstream message middleware such as Kafka and RabbitMQ, but also do not want developers to have to understand the differences in the usage of these middleware, then using the event-driven architecture based on Spring Cloud Stream is your first choice, because this framework provides a unified API externally, thereby hiding the differences in implementation of various middleware.

We do not intend to comprehensively introduce all seven technical systems in Spring. In the daily development process, if we are building a monolithic web service, we can use Spring Boot. If we want to develop a microservices architecture, then we need to use Spring Cloud based on Spring Boot, and Spring Cloud also includes an event-driven architecture based on Spring Cloud Stream. At the same time, here I want to specifically emphasize the Reactive Programming technology. Reactive Programming is the biggest innovation introduced in Spring 5, representing a technical direction for system architecture design and implementation. Therefore, today we will also focus on three technical systems: Spring Boot, Spring Cloud, and Spring Reactive Programming. Let’s see how Spring can specifically solve the problems for us in the development process.

Of course, all the technical systems of the Spring family that we can see now have gradually evolved from the Spring Framework. Before introducing the aforementioned technical systems, let’s briefly understand the overall architecture of the Spring Framework as shown in the following figure:

1.png

Overall architecture of the Spring Framework

Since its inception, Spring has been regarded as a container. The “Core Container” part in the above figure includes the core functionalities that a container should have, including JavaBean processing based on dependency injection mechanism in the container, Aspect-Oriented Programming (AOP), Context and the expression utility provided by Spring itself.

The two boxes at the top of the figure are the two most core functional components required to build an application and are also the most commonly used components in our daily development, namely data access and web services. These two major functional components contain a lot of content, which fully reflects the integration of Spring Framework. That is to say, the framework internally integrates various mainstream tools in the industry, such as database drivers, message middleware, ORM frameworks, etc., and developers can flexibly replace and adjust the tools they want to use according to their needs.

In terms of development languages, although Spring is most widely used in the Java EE field, the current version also supports Kotlin, Groovy, and various dynamic development languages.

Spring Boot and Web Applications #

Spring Boot is built on the foundation of the Spring Framework and is a new generation framework for developing web applications. We can understand the full picture of Spring Boot through the following figure:

图片2.png

Overall architecture of Spring Boot

By browsing the official website of Spring, we can see that Spring Boot has become one of the top-level subprojects in Spring. Since its release of version 1.0.0 in April 2014, Spring Boot has become the preferred framework for developing web applications in the Java EE field.

Let’s take a look at the coding work needed to develop an HTTP endpoint with a RESTful style using Spring Boot, as shown below:

@SpringBootApplication
@RestController
public class DemoApplication {

    @GetMapping("/helloworld")
    public String hello() { 
        return "Hello World!";
    }
   
    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

}
    

This is a classic “Hello World” program, and it only takes a few seconds to build such a RESTful web application using Spring Boot. Once a Spring Boot application is created and a class like the DemoApplication class above is added, we can start the built-in web server of Spring Boot and listen on port 8080. Spring Boot takes care of the rest for you, isn’t it powerful? The power of Spring Boot goes beyond this. Let’s take a look at the core features of Spring Boot together.

From the previous description, we have discovered that Spring Boot has features that traditional Spring frameworks do not have, namely support for embedded containers (including Tomcat, Jetty, and other traditional web containers that support the Servlet specification) at runtime. In the latest version of Spring Boot 2.x, it also provides support for Netty and non-blocking containers integrated with Servlet 3.1+. Based on the mechanism of embedded containers, developers can start web services with just one java -jar command.

From the previous code example, we also found that the core feature of Spring Boot is auto-configuration. In the previous example, we can see that Spring Boot does not require a large number of XML configurations related to HTTP requests and responses, like we used to in Spring MVC. In fact, the runtime process of Spring Boot still relies on Spring MVC, but it sets default values for various configuration items that developers need to specify and embeds them in the runtime environment. For example, the default server port is 8080. If we don’t require any customization of these configuration items, we can do nothing and adopt the established development conventions. This is the design concept advocated by Spring Boot: convention over configuration.

In addition, compared to traditional Spring frameworks, one of the highlights of Spring Boot is the introduction of built-in monitoring mechanisms, which are implemented through the Actuator component (common components include memory information, JVM information, garbage collection information, etc.). Based on the Actuator component, on the one hand, we can view detailed information about the application, including automatic configuration; on the other hand, we can monitor the runtime health status of the application in real-time.

As we can see, the above features of Spring Boot actually simplify the development process of web applications from multiple dimensions, including coding, configuration, deployment, and monitoring. In Lesson 02, we will provide more detailed descriptions for each dimension through a specific case.

Spring Cloud and Microservices Architecture #

Spring Cloud is built on the basis of Spring Boot, and its overall architecture is shown in the following diagram:

Drawing 3.png

Spring Cloud Architecture with Microservices (from the Spring official website)

The completeness of technical components is the main advantage of the Spring Cloud framework. It integrates a large number of well-known microservice development components in the industry. The core components of Spring Cloud are shown in the following diagram:

Drawing 4.png

Core components of Spring Cloud

As we can see, based on the development convenience of Spring Boot, Spring Cloud cleverly simplifies the development process of microservice system infrastructure. Spring Cloud includes service discovery and registration, API gateway, configuration center, message bus, load balancer, circuit breaker, data monitoring, and other components shown in the above diagram.

Spring 5 and Reactive Programming #

Currently, Spring has evolved to version 5.x. With the official release of Spring 5, we ushered in a new era of development in reactive programming. Spring 5 includes various reactive components related to data management, reactive message communication, and reactive web frameworks, greatly simplifying the development process and difficulty of reactive applications.

The following figure shows the comparison between the reactive programming technology stack and the traditional Servlet technology stack:

Drawing 5.png

Comparison between the Reactive Programming technology stack and the Servlet technology stack (from the Spring official website)

From the above figure, we can see that the left side of the above figure is the technology stack based on Spring WebFlux and the right side is the technology stack based on Spring MVC. We know that traditional Spring MVC is built on the Java EE Servlet standard, which is inherently blocking and synchronous, while Spring WebFlux is based on a reactive stream and can be used to build asynchronous and non-blocking services.

In Spring 5, Project Reactor was selected as the implementation library for reactive streams. Due to the characteristics of reactive programming, the operation of Spring WebFlux and Project Reactor depends on containers that support asynchronous mechanisms, such as Netty and Undertow. At the same time, we can also choose to use newer versions of Tomcat and Jetty as the runtime environment because they support asynchronous I/O of Servlet 3.1. The following figure more clearly illustrates the differences and connections between Spring MVC and Spring WebFlux:

3.png

In Spring Boot and Spring Cloud applications, Spring WebFlux and Spring MVC can be used in combination.

After discussing the Spring family’s technology ecosystem, let’s turn back to the course. In Lesson 01, we mainly discussed the Spring Boot framework in-depth from dimensions such as configuration system, data access, web services, message communication, system security, system monitoring, application testing, etc. The version used is 2.2.X.

Summary and Preview #

This lesson systematically analyzed the technical ecology of the Spring family and introduced the three major technology systems of Spring Boot, Spring Cloud, and Spring reactive programming. From the perspective of technological evolution and trends, the emergence of these tools and frameworks is inevitable. Spring Boot is an improvement over traditional Spring MVC, Spring Cloud is built on Spring Boot to construct a microservices architecture, and reactive programming represents the future direction of technology.