Programmers worship tech gods, in large part because of their ability to find and solve problems, especially when emergent problems arise online.
On the one hand, they have a deep technical foundation and know the application of the technology. On the other hand, they have accumulated a lot of experience in the process of mining.
I’m sure you’ve all used Spring, and some of you know the core of it: IOC and AOP, but only the basic concepts, reflection and dynamic proxies, how objects are managed, and how proxies are implemented.
Some people use Spring MVC to integrate databases, transactions, and message queues to simplify operations, but they don’t know much about the design and implementation of the integration.
I am also not good at this area, so I will sort out and summarize the technical principles related to Spring in the following period of time, with the book “Inside Spring Technology: In-depth Analysis of Spring Architecture and Design Principles” as the core, combined with my own experience and questions, as well as the great online articles, to make up for my lack of this aspect.
The author of this book is Ji Wenke. Based on Spring source code, this book reveals the design and architecture ideas of Spring from a macro perspective, and analyzes the implementation principle and operation mechanism of Spring function modules from a micro perspective. On the one hand, it is hoped that readers can better solve various problems encountered in practical development through in-depth understanding of the underlying mechanism of Spring, and on the other hand, it is hoped that readers can absorb excellent methods and ideas in Spring design and implementation.
The book consists of three parts, which respectively explain the core, components, and applications of Spring. The first part analyzes the IOC container and AOP implementation in detail, the second part describes the implementation of Java EE components in Spring based on the Spring IOC container and AOP, and the third part describes the implementation of some typical spring-based applications.
Here’s an overview of the design philosophy and overall architecture. Take a look at Spring’s design goals and architecture as a whole.
- Spring’s design goals
- The overall architecture of Spring
- Introduction to various Spring sub-projects
Design goals
In short, Spring is designed to provide a one-stop, lightweight application development platform that abstracts common problems encountered in application development.
As a platform, it takes into account the use of enterprise application resources, such as data persistence, data integration, transaction processing, message middleware, distributed computing, and other technical abstractions for efficient and reliable processing of enterprise data.
Lightweight is compared with the traditional J2EE, the traditional J2EE development, need to rely on the J2EE specification implementation of J2EE application server, the design and implementation, you need to follow a series of interface standard, this kind of development mode coupling is high, the application has an effect on the testability and deployment, the understanding of technology and the requirements are relatively high.
Using Spring for development, the developer is relatively light, can use POJO and JavaBean development way, make the application interface oriented development, fully support the object-oriented design method. Reducing direct coupling through the IOC container and increasing the capabilities of services in a dynamic and non-intrusive way through AOP provide the foundation for flexible selection of different service implementations, which is at the heart of Spring.
Common problems in the development process, Spring is packaged into various components, and Spring through the community, an open ecosystem, such as Spring Security from a community contribution Acegi.
The overall architecture
In general, Spring is divided into three layers. The bottom layer is the core layer, including IOC, AOP and other core modules, the middle layer is the encapsulated JavaEE services, as the driver components in the middle, and the top layer is each application.
The following is an architecture diagram from the Spring official website, showing its components:
Core container
Spring-core, spring-beans, spring-context, spring-context-support, and spring-expression modules:
Spring-core and Spring-Beans provide the foundation of the framework, including IOC functionality, and BeanFactory is an implementation of a complex factory pattern that decouples configuration and specific dependencies from actual program logic.
The Context module builds on the core and Beans modules, adding support for internationalization, event broadcasting, resource loading, and context creation. The ApplicationContext module is the focus of the Context module.
Spring-context-support provides support for a common third library, integrated into the Spring context, Examples include caching (Ehcache, Guava), communication (Javamail), scheduling (CommonJ, Quartz), template engines (Freemarker, Velocity).
The Spring-Expression module provides a powerful expression language for querying and manipulating object graphs at run time. The language supports property values, property parameters, method calls, array content storage, collections and indexes, logical and arithmetic operations, naming variables, and retrieving objects from Spring’s inversion of control container by name.
AOP and server tools
The Spring-AOP module provides section-oriented programming implementations, and the separate Spring-Aspects module provides aspectJ integration and applicability.
Spring-instrument provides some class-level tool support and classloader-level implementation for servers. Spring-instrument-tomcat Specifies the instrument implementation of Tomcat.
The message component
The Spring framework 4 contains spring-Messaging modules, abstracting from spring integration projects, such as Messge, MessageChannel, MessageHandler, and others used to provide message-based infrastructure services.
Data access/integration
The data access and integration layer consists of JDBC, ORM, OXM, JMS, and transaction modules.
The Spring-JDBC module provides A JDBC abstraction without the need to write verbose JDBC code and resolve database vendor-specific error code.
The Spring-TX module provides programmable and declarative transaction management.
The Spring-ORM module provides the leading object-relational mapping API integration layer, such as JPA, Hibernate, and so on.
The Spring-OXM module provides an abstraction layer to support implementations of Object/XML maping, such as JAXB, XStream, and so on.
The Spring-JMS module contains the ability to produce and consume messages, and since Spring4.1 provides the integrated Spring-Messaging module.
Web
The Web layer consists of spring-Web, Spirng-WebMVC, Spring-webSocket and spring-WebMVC-portlet modules.
The Spring-Web module provides basic Web development-oriented integration capabilities, such as multi-file uploads, and IOC container initialization using ServerT Listeners and Web development application contexts. It also includes HTTP clients and web-related support for Spring remote access.
Spring-webmvc contains Web applications implemented by Spring’s Model-View-Controller and REST Web Services.
The spring-webMVC-portlet module provides the portlet implementation of MVC mode. The biggest difference between Protlet and Servlet is that the processing of requests is divided into action and render stages. In a request, the action stage is executed only once. However, the Render phase may be executed multiple times due to the user’s browser actions.
test
The Spring-test module supports unit and integration testing by combining Junit or TestNG, providing continuous loading of the ApplicationContext and caching of these contexts.
Introduction of each sub-project
From configuration to security, from Web applications to big data — whatever infrastructure your application needs, there is a Spring project to help you build it, and Spring is modular in design.
Spring Boot
Simplify the initial setup and development process of new Spring applications and configure them in a specific way, eliminating the need for developers to define boilerplate configurations and achieving rapid development.
Spring Cloud
Tool set for distributed systems development, based on the Spring Boot, based on the JVM cloud application development of configuration management, service discovery, circuit breaker, intelligent routing, control bus, global lock, decision-making campaign, distributed session state management, cluster operations provides a simple way of development, which has many components:
- Distributed/versioned configuration: Spring Cloud Config
- Service registration and discovery: Netflix Eureka or Spring Cloud Eureka (secondary encapsulation of the former)
- Routing: Spring Cloud Zuul, based on Netflix Zuul
- Service-to-service invocation: Spring Cloud Feign
- Load balancing: Spring Cloud Ribbon is based on Netflix Ribbon
- Circuit breaker: Spring Cloud Hystrix
- Distributed messaging: Spring Cloud Bus
Spring Cloud Data Flow
Data Flow is a unified programming model and managed service for developing and performing large-scale Data processing, batch operations, and continuous operations.
Spring Cloud Data Flow is a redesign of Spring XD based on the native Cloud. The project aims to simplify the development of big Data applications. Spring XD’s stream processing and batch module refactoring are based on Spring Boot’s Stream and Task/Batch microservers, respectively. These applications natively support modern operating environments such as Apache YARN, Apache Mesos, and Kubernetes, all of which are automatic deployment units.
Spring Data
Data access module, which provides good support for JDBC and ORM. With the rise of NOSQL and BigData, there are more and more new technologies, such as non-relational database and MapReduce framework. In order to make spring developers more convenient to use these new technologies, through Spring Data, Spring provides a relatively consistent way for developers to access data in different types of datastores.
Spring Integration
In enterprise software development, Integration with external systems is often encountered. Spring Integration provides an extension to the Spring programming model that supports the enterprise Integration pattern, providing lightweight messaging mechanisms in applications, and integrating with external systems through declarative adapters.
There are several basic concepts in Spring Integraton:
- Message: Java object with metadata;
- Channel: a Channel for passing messages;
- Message Endpoint: The processing end of a Message. On the processing end, you can transform, route, filter, split, and aggregate messages.
You can also use a Channel Adapter, which is where the application interacts with the outside world. The input is Inbound and the output is Outbound. There are many connection types available. Examples include AMQP, JDBC, Web Services, FTP, JMS, XMPP, various NoSQL databases, and more. All of these things can be strung together with a simple configuration file for complex integration.
Spring Batch
Simplifies and optimizes batch operations for large amounts of data, supports transactions, concurrency, processes, monitoring, vertical and horizontal scaling, and provides unified interface management and task management.
For example, it provides many methods to read large files (such as 1GB CSV and XML files), load or update tens of thousands or even hundreds of thousands of records in the database. If all records are directly selected, the whole system will be brought down, but using Spring Batch, the framework will help him to retrieve part of the records for paging. Commit in batches when updating.
Spring Security
A Spring authentication and security tool. Its predecessor was Acegi, whose goal was to provide a security service for Spring applications, such as user authentication, authorization, and so on.
It uses the Filter in the Servlet specification to secure Web requests and restrict URL-level access, and can also secure method invocations using Spring AOP — with object proxy and usage advice to ensure that only users with the appropriate permissions can access secured methods.
It is flexible enough to authenticate users based on a variety of data stores. It has built in a variety of common user storage scenarios, such as memory, relational databases, and LDAP, as well as the ability to write and insert custom user storage implementations.
Spring HATEOAS
To understand HATEOAS: REST is an architectural style of distributed Hypermedia applications. The concept of HATEOAS(Hypermedia as the Engine of Application State) is mentioned in this paper. Hypermedia is an application state engine that provides links in the returned results. Connecting to other API methods allows the user to know what to do next without looking up the documentation.
An example of a non-Hateoas response to an article is:
GET /posts/1 HTTP/1.1 Connection: keep-alive Host: blog.example.com {"id" : 1, "body" : "My first blog post", "PostDate" : "2015-05-30t21:41:12.650z"}Copy the code
An example of a HATEOAS response is:
{
"id" : 1."body" : "My first blog post"."postdate" : "The 2015-05-30 T21: his 650 z"."links": [{"rel" : "self"."href" : http://blog.example.com/posts/1,
"method" : "GET"}}]Copy the code
Spring HATEOAS provides support to simplify things like checking in or getting hyperlinks.
Spring Rest DOCS
You can generate RESTful Service documents that are accurate and readable, and Spring official documentation is generated using Spring REST Docs.
Generating document fragments based on unit tests does not invade the source code, so it does not make the source code more and more bloated. Markdown is supported. Modifying one line of configuration code can support generating document fragments with Markdown syntax.
By default, at build time, unit tests are run first to generate snippets of documentation. Then, at package time, the final documentation is generated by adding the Asciidoctor-Maven-plugin, which is automatically updated with each release of the version as long as it is a standard development process.
Spring Social
One of the biggest benefits of Using Spring Social is that it already provides support for major Social networks, which can be easily configured, and for less-used Social networks, components can be found in the community.
Spring AMQP
AMQP messaging solutions based on the Spring framework provide a templated layer of abstraction for sending and receiving messages, provide message-driven POJOs, and make it easier to use AMQP message servers in Spring applications. SpringSource’s Rabbit MQ is an open source AMQP-based messaging server.
Spring Web Flow
Spring Web Flow is an extension of Spring MVC that enables the development of process-based applications. It separates the definition of a process from the classes and views that implement the behavior of the process, has simultaneous processing of multiple HTTP requests, manages session state, data transactions, supports AJAX to build a rich client experience, and provides support for JSF.
Spring LDAP
Spring LDAP is a Java framework for operating on LDAP. It is based on Spring’s JdbcTemplate pattern to help simplify things for developers.
Spring Session
Spring Session provides a solution for creating and managing Servlet HttpSessions. Spring Session provides the cluster Session function. By default, the external Redis is used to store Session data to solve the problem of Session sharing.
Spring Shell
Spring Shell provides an interactive Shell that lets you develop commands using a simple Spring-based programming model.
Spring Kafka
Spring for Kafka encapsulates and integrates native Kafka client consumer.
Spring Statemachine
Its main function is to help developers simplify the development process of the state machine and make the structure of the state machine more hierarchical.
Spring IO Platform
Think of it as a dependency maintenance platform that aggregates related dependencies and provides a version number for each dependency.
The main purpose is to solve the problem of dependency version conflict. When using Spring, we often use third-party libraries. Generally, we choose a version number or the latest one based on experience, and it is easy to conflict.
The Spring IO Platform solves these problems by automatically picking the best version for you without having to write a version number when adding third-party dependencies.
Please scan the QR code below and follow my personal wechat official account for more articles