Spring 5.x Source trip thirty-eight getBean processor extensions forty

Figure no less

Here Insert Picture Description

InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation

After instantiation, populateBeanthe beginning, do some processing, if the return is false, it would stop populateBean, return directly, which means you can control whether to continue implantation process.
Here Insert Picture Description

Extended combat

We come to a processor, let them inject the data.

MyConfig

Which defines a to be injected NoPopulateBean.

@Configuration
@ComponentScan(value = {"com.ww.pojo","com.ww.postprocessors"})
public class MyConfig {
    @Autowired
    private NoPopulateBean noPopulateBean;
    @Autowired
    public void setMyTestBean(NoPopulateBean noPopulateBean) {
        System.out.println(noPopulateBean);
    }
}

NoPopulateBean

@Component
public class NoPopulateBean {

}

Test code

    @Test
    public void postProcessAfterInstantiationTest() throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();
        MyConfig bean = applicationContext.getBean(MyConfig.class);
        System.out.println(bean);
    }

Take a look at the general result, there is injected:
Here Insert Picture Description
Here Insert Picture Description

MyInstantiationAwareBeanPostProcessor

Got nothing to do, to achieve postProcessAfterInstantiation method

@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor
{
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return false;
    }


The result is not injected, the injection method is not implemented:
Here Insert Picture Description
Here Insert Picture Description

This extension point what use is it, except that you can make specified beannot inject outside, of course, can be reversed to operate beanit, pull it to you all.

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/105174528