Spring 5. X series of tutorials: meet all your imagination of spring 5 – continuous update

Time:2021-12-4

brief introduction

What makes the Java world better, programmers more friendly, and the baldness rate less high, so that programmers don’t have to 996 every day and have time to find a girlfriend?

It’s spring.

What makes enterprise Java applications easy to understand, reduces the entry threshold for Java programmers, and leads to the dilemma of Java programmers everywhere?

It’s spring.

What has changed from the uncrowned king into a de facto corporate standard that makes rule makers tremble?

It’s spring.

In short, spring satisfies all your imagination about Java programs. If you are a java programmer, you will fall into the arms of spring.

Spring 5. X series of tutorials: meet all your imagination of spring 5 - continuous update

Today’s document is a summary of spring foundation. A small colored egg is attached at the end of the article. Welcome to pull back!

Spring foundation

Spring was born in 2003 as a modification of the previous complex J2EE specification. Some people think that spring and J2EE are competitive. In fact, they are complementary.

The latest version of spring is 5.3.0. By using spring, we can easily create enterprise applications. Spring also provides support for groovy and kotlin. After spring 5.1, jdk8 or above is required to run correctly.

For more information, please visit www.flydean.com

Spring in a narrow sense refers to the spring foundation, as shown in the following figure: spring framework runntime:

Spring 5. X series of tutorials: meet all your imagination of spring 5 - continuous update

Spring in a broad sense refers to the whole spring family, including spring boot, spring cloud and other projects.

Here we discuss the spring framework in a narrow sense. It can be divided into the following modules:

  1. Core – core is the core of spring, mainly including IOC container, events, resources, I18N, validation, data binding, type conversion, spiel, AOP, etc.
  2. Testing – testing is a very important module in spring, mainly including mock objects, testcontext framework, spring MVC test, webtestclient, etc.
  3. Data access – this part is related to database access, including transactions, Dao support, JDBC, O / R mapping, XML marshalling, etc.
  4. Web servlet – traditional support for web servlets, including spring MVC, websocket, sockjs, stomp messaging, etc.
  5. Web reactive – this is a new support for responsive systems in spring 5, including spring Webflux, webclient, websocket, etc.
  6. Integration – supports third-party systems, including remoting, JMS, JCA, JMX, email, tasks, scheduling, caching and other commonly used third-party systems.
  7. Languages – finally, support for other JVM languages, including dynamic languages such as kotlin and groovy.

The spring framework is divided into many modules. We are free to choose which module to use when building applications. The core module is the core container: including configuration module and dependency injection mechanism. In addition, the spring framework provides basic support for different application architectures, including messages, transaction data, storage and websites. It also includes servlet based spring MVC web framework and the latest responsive spring Webflux reactive web framework.

Core technology

The core technology of spring is the most important content in spring, and the most important in the core is the inversion control (IOC) container of the spring framework.

By using aspect oriented programming (AOP) technology in spring, IOC has been fully applied in spring. And spring has built its own AOP framework. At the same time, spring also provides the integration of the third-party AOP framework AspectJ, so as to meet the use of users with different needs.

IOC is also called dependency injection (DI). It refers to the process that an object defines its dependencies (i.e. other objects working with it) only through constructor parameters, factory method parameters, or after constructing or returning an object instance from a factory method, through the properties set on it. These dependencies are injected when the container creates the bean.

This process is the opposite of the way that the bean controls instantiation itself or locates its dependencies by directly constructing a class or service locator pattern (so it is called control inversion).

  • Spring 5 Reference Guide: IOC container
  • Spring 5 Reference Guide: dependency injection

Bean is a business component in spring. We complete the final business logic function by creating various beans.

Inside the container, the definition of each bean can be expressed as beandefinition. Through beandefinition, you can obtain a lot of bean information, including package name, bean scope, life cycle, bean reference and dependency, etc.

  • Spring 5 Reference Guide: bean creation
  • Spring 5 Reference Guide: bean scope
  • Spring 5 Reference Guide: bean lifecycle management
  • Spring 5 Reference Guide: container extensions
  • Spring 5 Reference Guide: annotation based container configuration
  • Spring 5 Reference Guide: component scanning
  • Spring 5 Reference Guide: JSR 330 standard notes

From the initial process oriented programming to the later object-oriented programming, the programming method has undergone significant changes. Object-oriented programming encapsulates the business logic into objects one by one, and all operations are transformed into operations on objects.

Object oriented programming is now widely used. Generally speaking, it is supported by high-level languages, but it also has disadvantages. For example, if we want to do some operations across objects (such as counting the execution time of a method in each object), object-oriented can be out of control. We may need to add some time statistics code to each object, so is there a better way to deal with this problem?

Aspect oriented programming (AOP) complements object-oriented programming (OOP) by providing another way of thinking about program structure. The key unit of modularization in OOP is class, while the modular unit in AOP is aspect. Aspects support modularization of concerns (such as transaction management) across multiple types and objects. (in the AOP literature, this concern is often referred to as a “crosscutting” concern.)

  • Spring 5 Reference Guide: introduction to spring AOP
  • Spring 5 Reference Guide: AspectJ annotations
  • Spring 5 Reference Guide: schema based AOP
  • Spring 5 Reference Guide: AOP proxy
  • Spring 5 Reference Guide: configurable for AspectJ advanced programming

Generally speaking, a bean contains a default parameterless constructor and get and set methods of properties.

Org.springframework.beans package contains a very important class called beanwrapper interface and its implementation beanwrapperimpl. Beanwrapper provides the functions of setting and obtaining attribute values (single or batch), obtaining attribute descriptors and querying attributes to determine whether they are readable or writable.

In addition, beanwrapper also supports nested attributes, allowing the attributes of child attributes to be set to infinite depth. Beanwrapper also supports adding standard JavaBeans properties propertychangelisteners and vetoablechangelisteners without supporting code in the target class. Finally, beanwrapper provides support for setting index properties. Beanwrapper is usually not used directly by application code, but by databinder and beanfactory.

  • Spring 5 Reference Guide: beanwrapper and property editor

Spring provides a convenient event processing mechanism, including event class applicationevent and event listening class applicationlistener. It implements the designer mode. If the bean that implements the applicationlistener interface is deployed to the spring container, the bean will be notified every time the applicationevent is published to the ApplicationContext.

  • Spring 5 Reference Guide: Event

The full name of spiel is spring expression language. It is usually used to facilitate evaluation in XML or annotations. It can be used by writing #{} such a format.

  • Spring 5 Reference Guide: spiel
  • Spring 5 Reference Guide: Environment

Spring defines a resource interface to access resources. Generally speaking, resources can be loaded from external links in the form of URL and found from the system itself in the form of file.

  • Spring 5 Reference Guide: Resources

Testing test

The spring team advocates Test Driven Development (TDD). The spring team found that the correct use of inversion of control (IOC) will certainly make unit testing and integration testing easier (because the existence of class setter methods and appropriate constructors make it easier to connect them together in testing without setting the service locator registry and similar structures).

Examples to be continued

Data Access

It is convenient to manage things through spring, and provides a consistent Dao interface for various data access technologies (such as JDBC, hibernate or JPA), so that you can easily switch between the above persistence technologies without considering the adaptation of various data access technologies.

Examples to be continued

Web Servlet

Spring provides the spring MVC framework to support the web. Like many other web frameworks, spring MVC is designed around the front-end controller mode. In this mode, the central servlet dispatcherservlet provides a sharing algorithm for request processing, and the actual work is executed by configurable delegate components. The model is very flexible and supports a variety of workflows.

Like any servlet, you need to use Java configuration according to the servlet specification or declare and map the dispatcher servlet in web.xml. Conversely, dispatcherservlet uses spring configuration to discover the delegate components required for request mapping, view parsing, exception handling, etc.

  • HTTP caching in spring MVC
  • @The difference between sessionattributes and @ sessionattributes

Web Reactive

In addition to the support for traditional web servlets, spring also introduces Webflux framework and responsive webclient to support reactive systems.

The Webflux framework is non blocking and needs to run on servers such as netty, undertow and servlet 3.1 + containers.

Examples to be continued

summary

Once I spent a childish few months translating the official spring 5. X documents sentence by sentence. Now it is dedicated to you. You are welcome to download it. If you can, please pay attention to me and give me some praise. Thank you:

Spring framework 5. X Chinese guide

Friends who don’t want to download can directly read the spring framework documentation Chinese document reading link through this link

Author: what about the flybean program

Link to this article: http://www.flydean.com/spring5/

Source of this article: flybean’s blog

Welcome to my official account: the procedures, the more wonderful things waiting for you!

Recommended Today

IOS development crash defense

1. Crash may occur in IOS Unrecognized selector sent to instance Unrecognized selector sent to class KVO Crash KVC Crash NSNotification Crash NSTimer Crash Container crash (crash caused by collection class operations, such as array out of bounds, inserting nil, etc.) Nsstring crash (crash caused by string operation) Bad access crash (wild pointer) Threading crash […]