02 Case Driven How to Analyze a Spring Web Application

02 Case Driven - How to Analyze a Spring Web Application #

In Lecture 01, we mentioned that the Spring family has many open-source frameworks that developers can use to build various Spring applications. In Lecture 02, instead of going into too much detail about the types and development methods of all these Spring applications, we will mainly focus on developing web-oriented services based on Spring Boot, which is the most common form of internet applications. Before introducing the development pattern based on Spring Boot, let’s first make a brief comparison with traditional Spring MVC.

Spring MVC VS Spring Boot #

In a typical web application, the frontend and backend usually use the HTTP protocol for request and response. During development, it is necessary to handle URL mapping, build HTTP requests, serialize and deserialize data, and implement the business logic within each service. The process is shown in the diagram below:

Drawing 0.png

HTTP request-response process

Now let’s take a look at the development steps required to complete the above process using Spring MVC, as shown in the diagram below:

Drawing 1.png

Development process of a web application based on Spring MVC

The diagram above includes steps such as defining Spring’s DispatcherServlet using web.xml, configuring the startup of Spring MVC, writing controllers to handle HTTP requests, and deploying the service to Tomcat web server. In fact, as web application development using traditional Spring MVC framework progresses, certain problems have been exposed. One typical problem is that the configuration work is too complex and burdensome, and there is a lack of necessary application management and monitoring mechanisms.

If we want to optimize this development process, there are several points worth exploring. For example, reducing unnecessary configuration work, automatically managing dependencies, simplifying deployment, and providing application monitoring. These points coincidentally drive the birth of a new generation of development frameworks represented by Spring Boot. The development process based on Spring Boot is shown in the following diagram:

Drawing 2.png

Development process of a web application based on Spring Boot

From the diagram above, we can see that there are noticeable differences between the development process based on Spring Boot and the one based on Spring MVC in terms of configuration management, service deployment, and monitoring. As a new member of the Spring family, Spring Boot provides exciting features. The core value of these features lies in ensuring the simplicity of the development process, which is reflected in multiple aspects, such as coding, configuration, deployment, and monitoring.

Firstly, Spring Boot makes coding simpler. We only need to add a dependency in Maven and implement a method to provide a RESTful API, which is advocated in the microservices architecture.

Secondly, Spring Boot makes configuration simpler. It transforms Spring’s XML-based functional configuration method into Java Config and provides .yml files to optimize the original configuration scheme based on .properties and .xml files. The .yml file is more intuitive and convenient for organizing configuration information, and it has stronger semantics. At the same time, based on Spring Boot’s auto-configuration feature, it provides default starter components for various common tools and frameworks to simplify configuration.

Finally, in terms of deployment, Spring Boot also introduces a new mode of one-click startup. The deployment package structure of Spring Boot is shown in the following diagram:

Drawing 3.png

Spring Boot deployment package structure

From the diagram, we can see that compared to the traditional mode of a war package, the Spring Boot deployment package contains both the business code and various third-party libraries, and it also embeds an HTTP container. This package structure supports one-click startup using the “java -jar application.jar” method, without the need to deploy an independent application server. The whole application can run through the default embedded Tomcat.

Finally, based on the newly provided Actuator component of Spring Boot, developers and operations personnel can obtain the current runtime status of the application through RESTful interfaces and monitor and alert for the underlying metrics behind these statuses. For example, the “/env/{name}” endpoint can be used to obtain system environment variables, the “/mapping” endpoint can be used to obtain all RESTful services, the “/dump” endpoint can be used to obtain thread work states, and the “/metrics/{name}” endpoint can be used to obtain JVM performance metrics, etc.

Analyzing a Spring Web Application #

For a web application developed based on Spring Boot, the code organization needs to follow a certain project structure. In Lecture 02, unless specifically mentioned, we will use Maven to manage the project structure and package dependencies. The project structure of a typical web application is shown in the following diagram:

Drawing 4.png

Spring Boot web project structure diagram

In the diagram above, there are several places that need special attention. I have also made specific annotations in the diagram, namely package dependencies, startup class, controller class, and configuration. Let’s discuss each of these parts separately.

Package Dependencies #

Spring Boot provides a series of starter projects to simplify the dependency relationships between various components. Taking the development of a web service as an example, we need to import the spring-boot-starter-web project, which does not contain any specific code, but includes some pom dependencies as shown below:

  • org.springframework.boot:spring-boot-starter
  • org.springframework.boot:spring-boot-starter-tomcat
  • org.springframework.boot:spring-boot-starter-validation
  • com.fasterxml.jackson.core:jackson-databind
  • org.springframework:spring-web
  • org.springframework:spring-webmvc

As you can see, this includes the spring-web and spring-webmvc components that are commonly used in traditional Spring MVC applications. Therefore, Spring Boot still relies on these two components to build the web request and response process at the underlying implementation.

If we use Spring Boot version 2.2.4, you will find that the Spring components it depends on have been upgraded to version 5.X, as shown in the following image:

Drawing 5.png

Dependency diagram for Spring Boot version 2.2.4

Introducing the spring-boot-starter-web component into the application is just like introducing a regular Maven dependency, as shown below.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Once the spring-boot-starter-web component is imported, we can fully utilize the automatic configuration mechanism provided by Spring Boot to develop web applications.

Bootstrap Class #

One of the most important steps in using Spring Boot is to create a Bootstrap class. The structure of the Bootstrap class is simple and relatively fixed, as shown below:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {

    SpringApplication.run(HelloApplication.class, args);

}

Obviously, a new annotation @SpringBootApplication is introduced here. In Spring Boot, the class with this annotation is the entry point of the entire application. On one hand, it starts the Spring container; on the other hand, it automatically scans for annotations such as @Component, @Service, @Repository, and @Controller under the code package structure and loads all the corresponding classes as Bean objects into the Spring container.

Controller Class #

The Bootstrap class provides the basic skeleton for our Spring Boot application. Next, we can add the access points for HTTP requests, which in Spring Boot is represented by a series of Controller classes. The Controller here is the same concept as in Spring MVC. A typical Controller class is shown below:

@RestController
@RequestMapping(value = "accounts")
public class AccountController {
 
    @Autowired
    private AccountService accountService;
 
    @GetMapping(value = "/{accountId}")
    public Account getAccountById(@PathVariable("accountId") Long accountId) {
        Account account = accountService.getAccountById(accountId);
        return account;
    }
}

Please note that the code above includes three annotations: @RestController, @RequestMapping, and @GetMapping. @RequestMapping is used to specify the mapping relationship for the requested URL. @GetMapping has the same effect as using @RequestMapping with GET requests. @RestController is an upgraded version of the traditional @Controller annotation in Spring MVC. It combines the functionalities of @Controller and @ResponseEntity annotations and automatically uses JSON for serialization and deserialization.

Configuration File #

We notice that there is an application.yml file under the src/main/resources directory, which is the main configuration file in Spring Boot. For example, we can add the following configuration information about the port, service name, and database access to this file:

server:
  port: 8081

spring:
  application:
    name: orderservice 
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/appointment
    username: root
    password: root

In fact, Spring Boot provides powerful automatic configuration. If there are no special configuration requirements, developers can use the built-in configuration system of Spring Boot to automatically integrate configuration information related to database access and other aspects.

Case Study: SpringCSS #

After introducing the basic process of creating a web application based on Spring Boot, we will now introduce the case system for this course: SpringCSS. CSS stands for Customer Service System, which is a common business scenario in e-commerce and health-related industries. We will demonstrate Spring Boot’s design concepts and various technical components by building a simplified but complete system.

Real-world customer service logic is usually very complex, but the purpose of the case system is to demonstrate the technical implementation process, rather than the specific business logic. Therefore, we have simplified the business process of the case, but all the technologies involved can be directly applied to daily development processes.

Overall Architecture of SpringCSS #

In SpringCSS, there is a customer-service, which is a Spring Boot application and the main service of the entire case system. In this service, we adopt the classic layered architecture, which means dividing the service into Web layer, Service layer, and Repository layer.

In a customer service system, the core business is to generate customer tickets. Therefore, the customer-service generally interacts with the user service account-service. However, since updating user account information is a low-frequency event, we design the implementation in a way that the account-service actively pushes user account change information to the customer-service through a message middleware, thereby completing the process of obtaining user information. As for the order-service, it is positioned as an order system, and the customer-service also needs to query order information from that service. The overall interaction process of the SpringCSS system is shown in the following diagram:

Drawing 6.png

Overall architecture of the SpringCSS system

The diagram above introduces the various technology components used in building SpringCSS. We will discuss these technology components in detail in the following lessons.

From Case Implementation to Principle Analysis #

Furthermore, one of the main goals of Lesson 02 is to help you complete the development of a web application program based on the Spring Boot framework through case implementation. However, this is not the only goal. As an extension, we hope to learn the working principles of the core components behind excellent open-source frameworks, and thus gain a deep understanding of the implementation principles of architecture.

In this course, we will analyze and understand the working principles of the core components in Spring Boot, such as the automatic configuration implementation principles, database access implementation principles, and HTTP remote invocation implementation principles, by familiarizing ourselves with the source code.

By deeply analyzing the source code and learning about the implementation principles of the above-mentioned core components, you will be able to master the methods and techniques involved in system architecture design and implementation, guiding your daily development work.

Summary and Preview #

Case analysis is the best way to master the application of a framework. This course is a Spring Boot application development course driven by case studies. Today, we provided a detailed introduction to the organizational structure and development methods of a typical Spring Boot web application, and introduced the SpringCSS case system, which runs through the entire course series.