Spring 5.x Source trip thirty-eight getBean processor extensions forty
Figure no less
InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation
After instantiation, populateBean
the 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.
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:
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:
This extension point what use is it, except that you can make specified bean
not inject outside, of course, can be reversed to operate bean
it, 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.