Introduction to Spring. This series of articles will explain how to use Spring and its core principles based on the latest Spring 5.x.

1 Overview of Spring

The Spring framework has been around since 2003, and so far it has evolved into an ecosystem or a technology, It includes Spring Framework, Spring Boot, Spring Cloud, Spring Data, Spring Security, Spring AMQP, etc.The project address of Spring’s official website). From configuration to security, from Web applications to big data — whatever your application’s infrastructure needs are, there is a Spring project to help you build it. Spring alone is like a toolbox, with a lot of tools inside, and we choose the right tools to implement our project’s needs.

Each Project corresponds to different functions, and a Project may be a collection of multiple modules to achieve similar functions. For example, the Spring Data project is a set of database access and operation function modules. Common modules include Spring Data JDBC, Spring Data JPA and other relational database modules, as well as Spring Data Redis and other non-relational database modules. Each module is designed to solve a specific and different problem. The idea of modularity is an important one in Spring, where each module can be used independently or in combination with other modules. When we use certain technologies in our projects, we just choose certain modules to use and don’t introduce other modules.

Spring benefits (from Spring official) :

  1. Spring is open source, has a vibrant community, is trusted and used by developers around the world, and has contributions from all the big names in tech, including Alibaba, Amazon, Google, Microsoft, and so on, without worrying that the framework will be left unmaintained or abandoned.
  2. The Spring Framework provides a simple development approach based on the Inversion of Control and Dependency Injection (DI) of the Spring Framework. This way of developing will avoid the numerous properties files and helper classes that can clutter the underlying code.
  3. Spring provides support for a variety of other excellent frameworks (Struts, Hibernate, Hessian, Quartz…) Direct support for different frameworks to integrate more smoothly.
  4. Spring is highly productive. Spring Boot changes the way programmers program Java, the idea of convention over configuration and embedded Web server resources, which fundamentally simplifies a lot of complicated work. At the same time, we can combine Spring Boot with Spring Cloud’s rich support libraries, servers and templates to quickly build microservice projects and perfect service governance.
  5. Spring is high-performance. Spring Boot can be used to start projects quickly, while the latest Spring 5.x support for non-blocking responsive programming can greatly improve response efficiency, and Spring Boot’s DevTools can help developers iterate on projects quickly. For starters, you can even use Spring Initializr (start.spring.io/) to start a…
  6. Spring is secure. Spring Security makes it easier for you to integrate with industry standard Security solutions and provide trusted solutions with default Security.

2 Overview of the Spring Framework

The literal translation of Spring Framework is “Spring architecture”. In fact, at the beginning, Spring Framework refers to some functions in the current Spring Framework. Later, Spring supports more and more functions. Some of Spring’s core functionality was extracted as a Spring Framework project and some similar functionality was isolated as a separate project. It can be said that the Spring Framework is the cornerstone of the entire Spring ecosystem. Other projects and other integrated boxes rely on the Spring Framework, especially Core Technologies.

The Spring Framework “provides everything you need to use the Java language in an enterprise environment.” Using the Spring Framework, you can quickly develop a complete enterprise application! It is also modular, and the application can choose the required modules.

In fact, Spring in a broad sense refers to the whole Spring family and all projects, including Spring Boot, Spring Cloud, etc., which can be regarded as an ecosystem, while Spring in a narrow sense refers to the Spring Framework. Other projects such as Spring Boot, Sping Cloud and so on are all based on the Spring Framework! Let’s use Spring to refer to the Spring Framework.

Spring is now in its fifth major release, and the latest Spring 5.x has the following modules:

  1. Core: The Core technology module on which all Spring Framework components rely to function properly. Including IoC containers (dependency injection, inversion of control), events, resources, I18N, validation, data binding, type conversion, SpEL, AOP… . The core technology module is required when we use other modules! It provides the most basic Spring functionality support.
  2. Testing: Test support module. Include Mock Objects, TestContext Framework, Spring MVC Tests, WebTestClient (Mock Objects, TestContext Framework, Spring MVC Tests, WebTestClient).
  3. Data Access: database support module. Including Transactions, DAO Support, JDBC, ORM, Marshalling XML (Transactions, DAO Support, JDBC, O/R Mapping, XML Marshalling).
  4. Web Servlet: A Web framework support module based on the Servlet specification. These include Spring MVC, WebSocket, SockJS, STOMP Messaging. They are synchronous blocking communication.
  5. Web Reactive: Support modules based on responsive Web frameworks. Including Spring WebFlux, WebClient, and WebSocket. They are asynchronous non-blocking (reactive) communication.
  6. Integration: Third party functionality support module. Services include remote processing, JMS, JCA, JMX, email, tasks, scheduling, caching, and more.
  7. Languages: Other JVM-based language support modules. Including Kotlin, Groovy and other dynamic languages.

The code in the Spring Framework 5.x version has now been updated to use new features in Java8, such as static methods for interfaces, lambda expressions, and streams. Therefore, if you want to use Spring 5.x, you require developers to have a minimum version of JDK8 and a maximum support of JDK11. Java SE 8 UNDape 60 is officially recommended as the minimal version of Java 8!

Spring 5 introduces Spring Web Flux, which is a better non-blocking responsive Web programming framework and can handle large numbers of concurrent connections better without relying on Servlet containers or calling Servlet apis. Can run on a server that is not a Servlet container (such as Netty) and is intended as an alternative to Spring MVC. Because Spring MVC is a Web framework for synchronous blocking I/O built on Servlet APIS, this means that it is not suitable for handling large amounts of concurrency, but currently Spring 5.x still supports Spring MVC.

Spring 5.x is partially compliant with JavaEE 7 specifications and compatible with JavaEE 8. For example, it follows the following specifications:

  1. Java Servlet 3.1 JSR 340: Spring supports pure Servlet API programming, but Spring has its own Spring MVC.
  2. Java API for WebSocket JSR 356: Spring supports pure WebSocket API programming.
  3. Java API for JSON Processing JSR 353: Spring supports the JavaJSON format specification.
  4. JSR 914: Supports the JMS Message protocol API.
  5. Java Persistence 2.1 (JPA) JSR 338: Java Persistence standard specification based on O/R mapping. Spring supports pure JPA specification programming, but Spring has its own Spring Data JPA.
  6. Dependency Injection JSR 330: Dependency Injection annotations. But Spring has its own set of more powerful annotations.
  7. Additional specifications …………

Now let’s learn about the Spring framework. The current Quickstart tutorial on the Spring website has been replaced with the Spring Boot project. Spring Boot is based on the idea of convention over configuration. Compared with traditional Spring projects, it provides an out-of-the-box programming experience, greatly reducing the work of developers to write configuration files and hiding a lot of primitive things. Beginners may get a better experience directly using Spring Boot to quickly build projects! If we just want to “know how to use”, then Spring Boot is a good place to start (and the Spring Boot tutorial will be updated later)!

In order to learn more in-depth, now we still start to build the Spring project from the handwritten configuration file, starting from the initial Spring Framework Core module, starting from IoC, gradually add modules and functions, and finally we use Spring Boot to build the project. In this way, we can not only understand the core principles of Spring, but also better experience the benefits of Spring Boot for developers. Instead of starting with the JAR version, we will use Maven as a project management tool, introducing jars in a coordinate manner, so if you don’t know Maven, learn maven first!

This tutorial series is divided into two parts:

  1. The first part is based on the latest (2020.09) Stable version of Spring 5.2.8.RELEASE. It starts with the basic functions of Spring core and mainly explains how to use them. A Chinese tutorial will be provided according to the official documentation of Spring, but it is not a direct translation of the official documentation. First of all, this tutorial is to use Chinese to integration and then display of knowledge, the second will tell some made no mention of the official document or too simple to supplement knowledge points, and for some is not common knowledge to give (because of the key points in the document number and miscellaneous, but we can’t for most knowledge is used). In addition, the most important thing is to provide a complete case for most of the explained knowledge points (cases in official documents are mostly illustrative cases).
  2. After speaking of a large module knowledge and usage, the important principles of the source code will be analyzed. The IoC container module, for example, the first thing we will speak of the IoC bean definitions and injection, the definition of attribute dependence and injection, the XML configuration, comments configuration and configuration of Java code, and so on core usage, then explain the IoC container start principle, principle of bean instantiation and dependency injection, and so on important process source (not all processes, Because there is so much Spring source code, you have to learn how to choose.

Related articles:

Spring. IO /

Spring Framework 5.x learning

Spring Framework 5.x source code

If you need to communicate, or the article is wrong, please leave a message directly. In addition, I hope to like, collect, pay attention to, I will continue to update a variety of Java learning blog!