Spring Leader Share: Spring Framework's Revisited on Core Container

Special Note

This is a made simviso video about the Spring Framework 5.2 documentation translated version of the content will be shared team sharer is Spring Framework 5.2 project leader. You can easily locate content quickly look back at some point in the future when.

Video Address:

[Foreign share cutting-edge technology - backend - Chinese subtitles] Further Discussion on the Spring Framework Core Container

Video copyright simviso all translated text, without authorization, please do not reprint :

Incidentally recommend a professional programmer back-end micro-channel group circle:

Introduction

Well, we started

Welcome back to this room, let us enter together to share part of Spring 5.2 in the past

Spring morning in the past 15 years to share this theme I see that you have in

In short, I am grateful to see you again

So, it is very suitable for the contents of the next talk, of course, this part of the independent existence

We are going to introduce us to use the latest technology in the condition Spring Framework 5

Especially after 5, I went through our theme in Spring Framework 5.0 and 5.1 and the design decisions made, which is closely related to the overall theme of today

The title has been given, namely Revisited Core Container Spring Framework's

Spring Spring Framework as an open source project, you can find from the github

This project contains the entire core web stack, namely web Mvc and webflux

This is my work as a content Spring Framework project leader usually done

Therefore, the relevant contents of all Spring Framework project are within the scope of my responsibilities

Not only as a technical director, they release fewer managers, but also the Spring Framework to coordinate matters related to major link

ok, the current Spring 5.2 is still under development

The next thing I will discuss also not fully formed, I hope that what we discuss today the greatest degree of retention

However, Spring Framework 5.2 RC will launch the first full version in July

so, then you will soon see

GA version (general availability) will be launched in September

ok

I have here a few themes through their centralized demonstration

Let's look at the core container, we will look at several different angles did what Spring Framework

First, let's see what we do with the core API

Iteration for the core API that every year we do one thing

For us, the main versions significance in our version control scheme in that it represents our minimum should be based on the standards developed at the same time we need to re-examine the entire framework code base and the entire framework API level

That these core API will conduct large-scale iterations, in fact, these big changes will only take place in the 3, 4, 5 These major release in Spring

Java 8-style evolution

Iteration core API is based on the Spring Framework 5 as a reference of Java 8

Entire framework code base, in particular all its API and SPI are using the Java features 8

Java 8 has done a lot to achieve implicit support (we do not need to bother to achieve), even in the Spring Framework 4 especially in the Spring Framework 3, we already had this support (automated implicit realization)

So you rarely have noticed this framework is not Java-based framework for the entire 8 feels like to use the same Java-based 8 (Spring Framework 4 is one such)

But Spring Framework 5 is really based on, in a few cases you will not feel the same as we have done a lot of code optimization within the Java 8, it is interesting that many optimizations contribution from the community

In the above Java 8 upgrade code style and implementation style to do a lot for our contribution

Inefficiencies can be resolved through Java8, which means this process of change we will continue Accordingly, I am also concerned about the numerous code contributions in this regard

We even have a contributor from Russia, sent 50 Pull Request to us in about three months time

All of these are related to the Java code style that we missed about 8 upgrade

From the point of view of our own vision, our best to do whatever has changed, of course, the process we use the tools of conveniently

But there is still room for improvement

Glad to be able to accept this community

Then, we get down to business

In the framework of its own API, we can finally Java8 based on our core designing method

so, as in the java.util package optional, stream interface, and functions (eg: supplier, consumer, predicate) and the like can now be used in the core framework api

You can refer directly to the core framework in your code api, or you can also implement these interfaces

Let's discuss some of these types of API could be used in our code as BeanFactory, JdbcTemplate, TransactionSynchronization

So here a lot of changes, we can now iterate adapter overloaded methods directly through some function interface (java.util.function)

Overloads accepts Java.time type (e.g. to replace java.util.Date or milliseconds (long) using Instant, Duration and Clock)

In the entire code base, these revisions API only on the surface, we do not usually do this but we have to try to find all the places that could be improved in the entire core framework libraries iterative upgrade alone

In addition to reference Java 8 API type, there are other very important things in Java 8 language provides a new feature is the default method interface (default)

You may have noticed (perhaps not noticed) interface to the default behind these methods give us what kind of benefits

In particular, many prior methods corresponding interface based implementations can be removed out

You may be interested to achieve a just for a particular interface, or two, or three, or four methods, now without the use of adapters associated implementation mode

You only need to directly implement the interface method on it, take TransactionSynchronization speaking, we only need to rewrite afterCommit () method while other methods simply be ignored, the default interface has been achieved. This is also the use of Java 8 default method of characteristics

This is our API designers, this is indeed a weapon in our hands

If we want to introduce new methods in the interface, we can default as the primary means of achieving

You do not need to achieve it, we can provide a default method for existing implementations can still continue to use

Here, we just use a few common types (BeanFactory, JdbcTemplate, TransactionSynchronization) to make a simple expression of the previous content

Nullability treatment

In our API revision Another equally important part of treatment is to be empty (null processing capabilities, Nullability)

Prior to this, the way we achieve and Java itself is the same

In not too many explicit statement of the time, some of the old Java code farmers could not say where short-selling or shorting so relative, the return value may be empty or may not be empty

This is the old way of processing can be null reference in Java

Show a completely different pattern in the Spring Framework 5

It provides a rigorous statement to be empty (@Nullable) and non-empty (@NonNull) of

By default it can be used in all packets, packet core framework and exceptions, so you can add @Nullable comment at all on something can be NULL

@Nullable constructor parameters can be used, method parameters, particularly the return value can also be used (such as in ObjectProvider getIfAvailable () method)

In the second iteration, we can @Nullable applied to our field, it can also be used to test our hypotheses by the current state of a particular object

This is a very useful practice

Turned out to be a very useful exercise for us, it can make sure we do not miss any potential treatment can be empty in the code

While eliminating the inefficiencies

But also it can help us find redundant (repetitive Defense) Code

In any execution path, it would have been for those objects will never be checked to Null

How are we to know?

We use the most tools is InteliJ IDEA, which supports @Nullable notes, we only need to configure it to @Nullable InteliJ will immediately know @Nullable use rules in our spring, followed by InteliJ will provide us with ready to use out of the box inspection

More importantly, we call constant conditions & exceptions, that is, would represent an implicit judgment to achieve the conditions and exceptions

Suppose you have a code bug @Nullable aspects that IDEA can tell us the location of the problem by checking these out of the box

When it is allowed to Null, you do not need to be checked, while on the other hand you turn to Null was examined, that is to say, it is actually must not be Null

So this is mainly for Java-level things, because our core framework is implemented in Java

We use the most is the place to take it to check our code

If you choose to use with Spring and Kotlin, then the effect is more significant, of course Kotlin is also recommended language for Spring development support

Starting Spring Framework 5.2, by Kotlin compiler, you will have a more powerful model can be empty treatment

In Kotlin type system, if you have not @Nullable statement on the basic types and reference types, which means never allowed to be empty

Kotlin compiler will use its assertion (check / verification), made calls and verify that you specifically set in the Java API as

In contrast, another value in return if you are calling Java API, you want to achieve this effect, we must be particularly clear statement that it will never

By using those simple annotations in our code base, making our API framework of support for Kotlin become more friendly than ever

So there is a very common experience in the use of Kotlin development process that is all the Spring Framework API you can call this clearly has Nullability processing rules

In some scenarios, we even additions and modifications to the rules can be empty

So some API calls in the early Spring 5 version of the Framework, you may receive a null value

But in the current version 5 Spring Framework, we stepped up its constraints, this situation will not happen again now

Therefore, in some scenarios, the adaptability of our amendments to be empty of

This is also useful in terms of Java

If your project code based on Java and Spring 5, then you can do is go with the idea for your code to be checked

Because if your code calls the Spring Framework code that idea will understand @Nullable comment Spring code and determine its Nullability

Your idea will be to verify the condition Spring Framework-based code, but also pointed out that those conditions you set meaningless

So it is valuable for Java developers

By ObjectProvider to elaborate relevant iteration

Okay

Look at a few examples of API amended, ObjectProvider class has a new look adds a lot of new methods from the Spring Framework 4.3 Dao 5 (@Nullable and increased functional support)

This is a reference (the target can be acquired by it Bean), i.e., the processing may be indirect through its dependence on the target

You can comment or get injected by reference to this type of goal by BeanFactory.getBeanProvider ()

This way you will have a hand can get the type of target objects ObjectProvider

You can get the target instance by ObjectProvider, it returns NULL if not use it, just like here

According to the definition in (this) getIfAvailable (), if not available is returned Null, so it is marked with @Nullable

This example illustrates well be empty annotations (getIfAvailable ())

For getIfUnique () is the same, only in the target instance of a single rather than multiple instances of circumstances, you really get an object, it would not

These methods have existed before the Spring Framework4.3

In 5.2, we just add them in the comments, this can make it very clear and beautiful semantics, whereby they can return NULL

We took the opportunity to add overloaded methods

In order to never return Null, we specifically disclaim the getIfAvailable (Supplier) This overloaded method of passing parameters of type java.util.function.Supplier of Java 8 Lambda Expressions

Here if the target object is not available, it uses a default value means that the target object is obtained either from the BeanFactory, either get the default by Supplier, which means you never get Null

So this method signature above, we did not use @Nullable, the return value is not null

There is a more functional style ifAvailable (Consumer) method can be acquired if ObjectProvider target object, the operation (Lambda expressions) Cunsumer of execution

Functional style in Java is really nice

By ObjectProvider these overloaded methods, I believe you have strong feelings about the

These style changes, you can tell you what to do core container movement (functional action)

! [1565430004879] (Spring Framework of Revisited Core Container.assets / 1565430004879.png)

ObjectProvider also provides several methods for retrieving a plurality of matching instances

This is nothing surprising

To this end we have to integrate java.util.stream streaming operation

It provides two new methods stream () and orderedStream () in the ObjectProvider

Literally you can see how it works similar to Collection.stream ()

If you have used a set of streaming in Java8 style

This is a very similar pattern, it is not actually a collection of objects which is a ObjectProvider, multiple targets can be acquired by Bean instances it

Although it is not nominal collections, but it can be streamed as a collection of interacting like

In addition, when you start, you can configure an application context, you can also choose to program in this way in the Spring Framework 5

In Spring 5, you can do this level

When we combine stereotype annotations (commonly have four @ Component, @ Controller, @ Repository, @ Service) performed during assembly may find the classpath (refer to the figure) is replaced here by scanning classpath

as the picture shows

This is one hundred percent will happen, there is no background work

When you have a very clearly defined in Spring (Popular speaking, is the tree, so melon sown)

If you do not say we need to scan a class, then we will not

At the same time, generic as the main entry point is used to achieve these objectives GenericApplicationContext is a country with a class of 12-year-old

Now, we can get a goal we can set the type of example of a supplier as a parameter by such methods as registerBean (supplier)

Here we use some Java 8-style API

This registerBean (Foo.class) is a standards-based means to construct an instance, if there is no other explanation, then it is based on the default constructor

You may be more interested in our registration component of a Bar.class this registerBean (Bar.class, ...), with a Lambda expressions to create a new instance of Bar to us within inside

The inline expression is Java.util.function.Supplier realize it is somewhat like functional style factory method

Here and there is no factory method

There are only the inline Lambda expressions, no extra components, no class, no Bean method, no comment

Even need not reflected directly through an example Supplier

If this is a Prototype Scope component or components, each invocation of the Bar needs a new instance of our dynamic instruction in this call, we do not need to be reflected directly be obtained by calling this instance Supplier

Here are a few variants if you want, you can pass a ObjectProvider

We assume that such a Bar, which is that it can bind to (as shown) by way of a goal such programming Object object type or instance be constructed Foo.class

There are other little things

We look at the following variants

Lambda expressions which there are more examples of it not only has a Supplier (Bar can be used to create instances)

There is also a

In fact there may be more, we will call them BeanDefinitionCustomizer

Based on this, you can get a BeanDefinition (which we will want to register metadata objects) callback

These BeanDefinition can also be created by annotations, we can also be defined in XML Bean created

But here we are with the form code to implement

Before it is registered to this container, you can set some specific signs or qualifiers such as lazy-init or add some qualifiers in BeanDefinition

The Lazy and you add notes or add notes Qualifier or some other similar notes, resulting effect is the same

This is just to give the impression you leave is not the focus of today

All this also applies to Kotlin, everything we do in the Spring Framework 5 can be used in the Kotlin

Guess you like

Origin juejin.im/post/5d4f80f66fb9a06ae439e52c