The use of Spring AOP post processor

Get into the habit of writing together! This is the 15th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

The sparse shadows are horizontal and the water is clear and shallow, and the dark fragrance floats at dusk.

1 Introduction

In the Spring system, the understanding of the IOC container and Bean has been described in the previous article, and the source code sharing of AOP will be continued in this article. AOP is a very complicated knowledge point, so let's start with the post-processor.

2 BeanPostProcesser post processor

BeanPostProcesserIt is a very important concept in Spring, which is an extensible interface provided by the container. The annotations given by Spring about the post-processor are as follows: In BeanPostProcesser annotationshort, it is similar to a factory hook that allows users to customize and modify beans For example information, check the marked interface or wrap the bean as a proxy. Marker interfaces are generally processed before initialization, and in the case of proxies, after initialization. Mainly divided into two considerations: one is registration, which ApplicationContextcan automatically detect the post-processor Bean and apply it before and after the subsequently created Bean instance. One is the order. The interfaces of the post-processor will be processed in a certain order. The priority order from high to low is PriorityOrdered, Ordered, and non-sorted interfaces.

Use CasesIt can be seen here that although it BeanPostProcesseris an interface, it can also have method bodies, which is a feature of java8. It mainly includes the following two methods:

# 传入的参数是 bean 本身和 beanName 名称
# bean 初始化方法调用前被调用
postProcessBeforeInitialization(Object bean, String beanName)
# bean 初始化方法调用后被执行
postProcessAfterInitialization(Object bean, String beanName)
复制代码

Here, we need to talk Instantiationabout Initializationthe difference between instantiation and initialization. Instantiation is the process of creating a bean. This process generally calls the constructor, and initialization is the assignment of attributes to the bean. Let me introduce it first. It InstantiationAwareBeanPostProcessoris also inherited from BeanPostProcesser, but to achieve more functions, it needs to be executed before and after instantiation.

InstantiationAwareBeanPostProcessor 接口中定义的方法
# 实例化之前的处理
postProcessBeforeInstantiation
# 实例化之后处理
postProcessAfterInstantiation
# 对属性值进行修改,如果 postProcessAfterInstantiation方法返回false,该方法可能不会被调用。可以在该方法内对属性值进行修改
postProcessProperties
# 该方法已经废弃,其作用和 postProcessProperties 类似
postProcessPropertyValues
复制代码

InstantiationAwareBeanPostProcessorIt is still necessary to move out this relatively classic picture to illustrate the problem. The general process is shown in the figureDeclare the Bean AppInstantiationAwareConfigimplementation interface here InstantiationAwareBeanPostProcessor. You can see that the aspect function can be realized before and after the instantiation and initialization of the Bean during the project startup process.

3 Summary

In this article, I describe the front-end of AOP, the use of post-processors and the role of API. It is very helpful for the understanding of subsequent AOP. In subsequent articles, I will continue to share the core enhancement of AOP and its proxy. content.

Guess you like

Origin juejin.im/post/7086758514290491400