Spring 5. X Series tutorial: meet all your imagination of spring 5 – continuous update

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.

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. In spring 5 After 1, jdk8 or above is required to operate correctly.

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

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:

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 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. These dependencies will be injected when the container creates a 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).

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.

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, as long as it is supported by high-level languages, 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), the ability of object-oriented can not be caught. We may need to add some time counting code in each object. 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 classes, while the modular unit in AOP is aspects. Aspects support modularization of concerns (such as transaction management) across multiple types and objects. (in AOP literature, this concern is often referred to as "crosscutting" Attention.)

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

org. springframework. There is a very important class in the beans package called beanwrapper interface and its implementation beanwrapperimpl. Beanwrapper provides the functions of setting and obtaining property values (single or batch), obtaining property descriptors and querying properties 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 propertychangelisters 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 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.

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 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.

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 on the web Dispatcherservlets are declared and mapped in XML. Conversely, dispatcherservlet uses spring configuration to discover the delegate components required for request mapping, view parsing, exception handling, etc.

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>