BeanPostProcessor: It's an interface ,bean Post processor of ( It's actually in the bean Before and after initialization of )
public class MyBeanPostProcessor implements BeanPostProcessor{ /** *
bean Is an instance created by the container call constructor * beanName It's an example id * Call before all initialization methods */ public Object
postProcessBeforeInitialization(Object bean, String beanName) throws
BeansException { return bean; } /** * bean Is an instance created by the container call constructor * beanName It's an example id *
After all initialization methods are called */ public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException { return bean; } }
take MyBeanPostProcessor Incorporate into container :
@Configuration @Import(MyBeanPostProcessor.class) public class LifeCycleConfig
{ @Bean(initMethod="init0",destroyMethod="destroy0") public Car car(){ return
new Car(); } }
So that all bean The logic in the post processor is executed before and after initialization , Even if the initialization method is not defined, the logic is executed
, From this we can guess :BeanPostProcessor It may be faceted

By looking at the source code , The whole logic goes like this :

After calling the constructor , All properties of the object are assigned values , After the assignment is completed, the initialization method is called , However, when the initialization method is called, it will enter into an aspect , Completed before and after initialization BeanPostProcessor The logic of : The container will traverse all the BeanPostProcessor And execute the pre method of each post processor in turn (postProcessBeforeInitialization), Until all are executed or one of the class methods returns null, And then finish Bean Initialization of , After initialization , The container will continue to traverse and execute all of the BeanPostProcessor Post method of (postProcessAfterInitialization), Until all are executed or one of the methods returns null

JDK1.8 after interface There can also be methods , The subclass can also not implement methods that already have a method body in the interface :
public interface BeanPostProcessor { @Nullable default Object
postProcessBeforeInitialization(Object bean, String beanName) throws
BeansException { return bean; } @Nullable default Object
postProcessAfterInitialization(Object bean, String beanName) throws
BeansException { return bean; } }
BeanPostProcessor stay Spring Use of bottom layer :

① Components are implemented through ApplicationContextAware Interface acquisition IOC container , It is ApplicationContextAwareProcessor At work
@Component public class ContextBean implements ApplicationContextAware {
private ApplicationContext context; public void
setApplicationContext(ApplicationContext applicationContext) throws
BeansException { context = applicationContext; } }
How did you get it IOC What about the container ?
Container is calling Bean After all attributes have been assigned , Before and after executing all initialization methods , Will implement some BeanPostProcessor Logic in subclasses of an interface , such as ApplicationContextAwareProcessor, In execution ApplicationContextAwareProcessor Of postProcessBeforeInitialization() Method , Can judge Bean Is it realized ApplicationContextAware Interface , And then inject the corresponding ApplicationContext Container for Bean use

② To verify the properties of a component BeanValidationPostProcessor


InitDestroyAnnotationBeanPostProcessor: scanning Bean In the @PostConstruct and @PreDestroy Annotation method and implementation , That's why @PostConstruct The initialization method of the annotation is executed before other initialization methods


AutowiredAnnotationBeanPostProcessor: by bean In the @Autowired Attribute assignment of annotation , That's why you can be in init() Method @Autowired The reason for the attribute of the annotation , Because the init The values of these properties are injected before the

⑤AsyncAnnotationBeanPostProcessor: Make marked @Async The method is executed asynchronously

 

Technology
©2019-2020 Toolsou All rights reserved,
In the problem of target detection “ recall Recall”,“ Accuracy Precision”Linux Page replacement algorithm C Language implementation About wechat payment body( Product description ) Problems encountered stay Vue Use in Web Worker Calculate the working days of each month c++ Memory Pointer out of bounds detection mechanism _CrtMemBlockHeadeJavaScript Medium Call and Apply Remember once EventBus Project issues caused by memory leaks Hundreds of millions of locusts rarely collide Locusts want to be self driving Heroes Java Swing JList: List box components