Make writing a habit together! This is the 15th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Shuying oblique water shallow, incense floating on dusk.

1 introduction

In the Spring architecture, the UNDERSTANDING of the IOC container and beans has been covered in previous articles, and AOP source sharing will continue in this article. AOP is a very complicated topic, so I’ll start with the post-processor.

2 BeanPostProcesser post-processor

BeanPostProcesserAn important concept in Spring is that it is an extensible interface provided by the container. The comments on the post-processor Spring are as follows:Basically, it’s like a factory hook that allows you to customize the Bean sample information, check the tag interface, or wrap the Bean in a proxy way. Markup interfaces are typically handled before initialization, or after in the case of proxies. There are two main considerations: one is registration,ApplicationContextThe ability to automatically detect post-processor beans and apply them before and after Bean instances that are subsequently created. One is the sequence, in which interfaces of the rear processor are processed in a certain order: PriorityOrdered, Ordered, and unordered interfaces in descending order of priority.

You can see here thoughBeanPostProcesserIs an interface, but can also have a method body, which is a feature of java8. It mainly contains the following two methods:

# incoming parameter is the bean itself and beanName name # bean initialization method is invoked before invoking postProcessBeforeInitialization (Object bean, String beanName) # bean initialization method call is executed after postProcessAfterInitialization (Object beans, String beanName)Copy the code

Instantiation is the process of creating a Bean, which calls the constructor, and Initialization is the process of assigning attributes to the Bean. Here lie first introduce InstantiationAwareBeanPostProcessor is inherited from BeanPostProcesser, but realized more features, is a need to perform before, during, and after instantiation.

InstantiationAwareBeanPostProcessor interface methods defined in the # instantiated before treatment after postProcessBeforeInstantiation # instantiation PostProcessAfterInstantiation # modify attribute values, if postProcessAfterInstantiation method returns false, this method may not be invoked. Can be modified in the method of attribute value postProcessProperties # abandoned the method, its function and similar postProcessPropertyValues postProcessPropertiesCopy the code

Again, this is a classic picture,Declare the Bean hereAppInstantiationAwareConfigImplementing an interfaceInstantiationAwareBeanPostProcessor, you can see that the aspect function can be implemented before and after the Bean’s instantiation and initialization process during project startup.

3 summary

This article covers AOP’s precursors, post-processor-related uses, and the role of the API, which will be useful for understanding AOP in future articles that will continue to share AOP’s core enhanced fetching and proxying.