Product spring: call rules of bean factory postprocessor

Time:2020-1-9

The last article introduced the processing logic of @ configuration class, which is written in the configurationclasspostprocessor class.


This class is more than just a“Bean factory postprocessor”, or a“Bean definition registration post processor”。

These are actually two interfaces that operate on bean definitions. So it’s very important.

In other words, these two interfaces are the only ones that can operate on bean definitions. You say it’s not important.

Look at the type information below. There are only two interfaces in the whole spring, as shown in Figure 01:


Although they all do operations related to bean definition, the purpose is obviously different.

After the bean definition is registered, the processor is used to register the bean definition in the container. As a result, the number of bean definitions has increased.

As shown in Figure 02:


Its interface method is executed when all “regular bean definitions” have been registered, allowing further bean definitions to be added to the container.

New notes on programming: the “regular bean definition” here refers to the bean definition registered before the container refresh.

The bean factory postprocessor is used to modify the bean definition in the container, resulting in the same number of bean definitions.

As shown in Figure 03:


Its interface method is executed when all bean definitions have been registered and cannot be increased. This method allows to modify some properties of bean definitions.

In addition to being registered in the container as a bean definition, the postprocessor can also manually add the new instance to the container (bean definition is not registered at this time).

In the abstract class abstractapplicationcontext of the container, as shown in Figure 0405:


Next, we start calling the postprocessor in the refresh method, as shown in Figure 06 below:


Continue to follow the method, as shown in Figure 07:



It can be seen that there is a proxy class specifically responsible for calling the postprocessor methods, and the second parameter is the postprocessor instance we manually added (generally no one added, so it is empty).


In a program, “order” is a very important thing in many cases. The same code, different execution order, may be different results or errors.

In spring, there is a unified solution for sequence processing, that is, interface or annotation.

First, the ordered interface, as shown in Figure 08:


Using a value of type int to represent the order is simple.

It should be noted that the highest priority is the negative minimum, and the lowest priority is the positive maximum.That is, the smaller the value, the higher the priority.

Then there is the priorityordered interface, as shown in Figure 09:


It just inherits the ordered interface and does nothing.You can see from the name that the priority of this interface is higher than that of ordered interface.

Then there is the @ order annotation, as shown in Figure 10 below:


Starting from spring 4.1, the standard Java annotation @ javax. Annotation. Priority can be used as a temporary alternative, as shown in Figure 11 below:


In specific use, it is better to keep the same type of components in a uniform style, using interfaces or annotations.

In another case, it should be noted that a default order value will be given to a class that does not implement an interface or annotate a solution.

Generally 0 or maximum or minimum. In the middle, or the lowest priority, or the highest priority.


Now we start to call these postprocessors specifically. There are several sequential problems:

1)Call the manually added postprocessor first, then the postprocessor registered as the bean definition

2)First call the bean definition to register the postprocessor, and then call the bean factory postprocessor

3)First call the interface method defined by the registered bean, and then call the interface method defined by the modified bean

4)First call to implement the priorityordered interface, then call to implement the ordered interface, and finally there is no interface

The whole calling process is divided into many steps:

First stepFirst, call the manually added bean definition registration and then the processor’s registration bean definition method, as shown in Figure 12 below:


The second step, and then call the bean definition in the container that implements the priorityordered interface to register the bean definition method of the post processor, as shown in Figure 13 below:


The third step, and then call the bean definition in the container to implement the ordered interface, and then the registered bean definition method of the processor after registration, as shown in Figure 14 below:


The fourth step, and then call all the remaining beans in the container to define the registered bean definition method of the registered processor, as shown in Figure 15 below:


New notes on programming

Why is it called all the time through the loop here? Because this is to register the bean definition, and the registered bean definition may also be a bean definition post registration processor.

It’s easy to understand, just like buying another bottle of drinks.

You buy 10 bottles, open them all, one more bottle for 8, and the boss gives you another 8 bottles, open them all again, one more bottle for 5, and the boss gives you another 5 bottles, and you open them again.

So repeatedly, until did not encounter another bottle.

Up to now, all the methods for registering bean definitions have been adjusted, which means that the registration of bean definitions has been completed, and the number of bean definitions will not increase any more.

The fifth step, call the method of modifying the bean definition of the processor after all bean definitions are registered, modify or improve the bean definition as required, and the execution order is consistent with the above, as shown in Figure 16 below:


Up to now
After all bean definitions are registered, all processor interfaces have been called. Next, it’s time to call the bean factory postprocessor.

The sixth step, call the modified bean definition method of the post processor of the manually added bean factory, as shown in Figure 17 below:


The seventh step, call the bean factory postprocessor’s modified bean definition method to implement the priorityordered interface in the container, as shown in Figure 18 below:


New notes on programming

It can be seen that the writing method here is different from the above. The above is obtained from the container every time because the number of bean definitions has been increasing.

Now the number of bean definitions will not change any more, just get them from the container once, and a loop can separate them according to the different interfaces implemented.

The eighth step, call the bean factory postprocessor in the container to modify the bean definition method, as shown in Figure 19:


The ninth step, call the bean definition method of the bean factory postprocessor that does not implement the interface in the container, as shown in Figure 20 below:


Because there is no interface implemented, there is no sorting in this step.


Up to now, all bean definitions have been modified. There will be no more changes to the properties defined by the bean.

Sum up

This article introduces two “post processor” interfaces, one for registering bean definitions and one for modifying bean definitions.

This is also the only two interfaces in spring that can operate on bean definitions. So they are very important.

Then their calling order is also important, such as registering the bean definition before modifying it.

There are also applications for the priorityordered interface and the ordered interface, as well as applications without interfaces.

After the bean definition is registered, the processor uses a call mode similar to “another bottle”. Because the new bean definition may still be of this type.

The common way to call the bean factory postprocessor is because the number of bean definitions no longer changes.

So far, all the bean definitions are ready, waiting for the next phase.

 

(END)

 

>>>Article series on spring<<<

 

Spring: the foundation of Empire

Product spring: bean definition goes to Liangshan

Product spring: the “advanced productivity” used in bean definition

Tasting spring: annotation is finally “successful”

Pin spring: craftsman’s “blessing” on annotation

Product spring: is there any essential difference between spring boot and spring?

Product spring: two “pioneers” responsible for bean definition registration

Product spring: spring boot easily wins the “first stage” of bean definition registration

Product spring: spring boot initiates the “second battle” of bean definition registration

Pin spring: King of annotation @ configuration and its group of “little brothers”

 

>>>A collection of popular articles<<<

 

Ten years after graduation, I have something to say

[interview] how do I interview other people’s list related knowledge? It’s a little deep and long

How did I become the development team leader in just one year after graduation

My father gave birth to a younger brother for spring MVC, spring Webflux

[interview] how do I “routine” others when interviewing for spring affairs

[interview] spring affairs interview test site spitting blood (recommended Collection)

[interview] how do I “soft” him when interviewing others about redis

[interview] after eating through these redis knowledge points, the interviewer must think that you are very NB (recommended to store dry goods)

[interview] if you answer “what is thread safety” in this way, the interviewer will look at you with great admiration

[interview] a good article that has been told so clearly by synchronous / asynchronous / blocking / non blocking / bio / NiO / AIO so far (quick collection)

[interview] an article will help you to fully understand the past and present life of “I / O multiplexing” and “asynchronous I / O” (deep good article, recommended Collection)

[interview] if you treat the thread as a person, all the questions will be understood in an instant

Java multi thread customs clearance — basic knowledge challenge

Spring: the foundation of Empire

 

authorIt’s work over10 yearsNow he’s an architect. Like to research technology, advocate simple and happy.The pursuit of easy to understand language interpretation technology, hope that all readers can understand and remember.Below is the public code and the two-dimensional code of knowledge planet. Welcome to pay attention!