Spring 5.x Source trip thirty-nine getBean extension Processor 4.5

Figure no less

Here Insert Picture Description

InstantiationAwareBeanPostProcessor的postProcessProperties和postProcessPropertyValues

This is what we call it auto-assembly, internal CommonAnnotationBeanPostProcessorand AutowiredAnnotationBeanPostProcessorhandling processor for injection.
Here Insert Picture Description
Here Insert Picture Description
postProcessPropertyValuesIt has expired.
Here Insert Picture Description
Here Insert Picture Description
In fact, almost all of the processing. So we can be here in your own extensions, objects have got, and what can not do what is right, not specifically demonstrated.

initializeBean of invokeAwareMethods completion of the injection began initialized

Mainly set some properties, there is a more important is BeanFactoryAware, it can be obtained BeanFactory.
Here Insert Picture Description
For example, we own realization one can get this information, it is more simple look like:
Here Insert Picture Description

applyBeanPostProcessorsBeforeInitialization before initialization

If you return null, then return directly, that is not to continue to do a deal, so here can actually organize other processors to handle, custom processor directly back null, he returned.
Here Insert Picture Description
In fact, that is, for beansome operations, nothing special.
Here Insert Picture Description

invokeInitMethods perform initialization method

Mainly the implementation implements InitializingBeanthe interface afterPropertiesSetmethod:
Here Insert Picture Description

Some methods invokeCustomInitMethod custom

That is beandefined in the set initMethodName:
Here Insert Picture Description

Real

Just write the definition of a class method:

public class PoJo {
    public void init(){
        System.out.println("自定义initMethodName");
    }
}

Test code

Registered a bean definition is then set InitMethodName.

    @Test
    public void invokeCustomInitMethodTest() throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        AnnotatedBeanDefinition annotatedBeanDefinition=new AnnotatedGenericBeanDefinition(PoJo.class);
        annotatedBeanDefinition.setInitMethodName("init");
        applicationContext.registerBeanDefinition("myBean",annotatedBeanDefinition);
        applicationContext.refresh();
    }

Here Insert Picture Description

After initialization applyBeanPostProcessorsAfterInitialization

And similar treatment before initialization, do not say.
Here Insert Picture Description

Well, here today, we hope to help study and understand, do not spray the Great God see, understand only their own learning, limited capacity, please excuse.

Published 235 original articles · won praise 74 · views 30000 +

Guess you like

Origin blog.csdn.net/wangwei19871103/article/details/105175013