作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO
联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬
学习必须往深处挖,挖的越深,基础越扎实!
阶段1、深入多线程
阶段2、深入多线程设计模式
阶段3、深入juc源码解析
码哥源码部分
码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】
码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】
码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】
码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】
打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】
图不能少

obtainFromSupplier实例提供器
可以进行自己的提供器提供实例直接返回。


实战
要把我们这个干净的类注册到容器里:
public class MySupplier implements Supplier {
@Override
public Object get() {
MyBeforeInstantiation instantiation=new MyBeforeInstantiation();
instantiation.age=100;
return instantiation;
}
}
MySupplier实例提供器
public class MySupplier implements Supplier {
@Override
public Object get() {
return new MyBeforeInstantiation();
}
}
测试代码
测试代码:
@Test
public void MySupplierTest() throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(MyConfig.class);
applicationContext.registerBean("myBeforeInstantiation",MyBeforeInstantiation.class,new MySupplier());
applicationContext.refresh();
MyBeforeInstantiation myBeforeInstantiation = applicationContext.getBean(MyBeforeInstantiation.class);
System.out.println(myBeforeInstantiation.age);
}

SmartInstantiationAwareBeanPostProcessor的determineCandidateConstructors
createBeanInstance中的determineConstructorsFromBeanPostProcessors获取构造方法。这里主要是AutowiredAnnotationBeanPostProcessor可能会帮你挑选出Autowired注解的方法。但是我们自己扩展个试试。

只要有返回的构造器不为空,就直接返回了。

实战
里面有两个构造方法的时候,AutowiredAnnotationBeanPostProcessor会选择默认构造方法,所以输出应该是0。但是这次我要想让他使用第二个构造方法。
MyBeforeInstantiation
MyBeforeInstantiation我们要实例化的类:
@Component
public class MyBeforeInstantiation {
public int age;
public MyBeforeInstantiation(){
System.out.println("MyBeforeInstantiation()");
}
public MyBeforeInstantiation(PoJo poJo1,PoJo poJo2) {
System.out.println("BeforeInstantiation(PoJo poJo1,PoJo poJo2)");
}
}
//测试用的
@Component
public class PoJo {
}
测试代码
@Test
public void determineCandidateConstructorsTest() throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(MyConfig.class);
applicationContext.refresh();
}
结果输出:

MySmartInstantiationAwareBeanPostProcessor扩展处理器
我就指定让他返回第二个构造方法。
@Component
public class MySmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {
@Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
if(beanClass.getSimpleName().equals(MyBeforeInstantiation.class.getSimpleName())){
try {
return new Constructor[]{beanClass.getDeclaredConstructor(PoJo.class,PoJo.class)};
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return null;
}
}
结果:

我们成功改变了原有的设置。
疑问
但是有个问题,有没想过,为什么我们的处理器是在内部处理器AutowiredAnnotationBeanPostProcessor之前处理的,我们没有声明任何优先排序啊,其实这个是在注册处理器registerBeanPostProcessors的中将MergedBeanDefinitionPostProcessor类型的放入internalPostProcessors中了,而AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor刚好是这个类型的,他们最后又被注册了一遍,而注册的方法是会把存在的删除,然后把新注册的放最后。


这就解释了为什么我们的在他们前面了,本来是他们先注册进去的,只最后被重新注册到后面了,所以可以先执行我们的处理器,返回的构造器不为null就直接返回对象了。
当然如果你为了要保证顺序的话就实现PriorityOrdered接口吧,比如还有其他的一些处理器,得有个顺序对吧:
@Component
public class MySmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor, PriorityOrdered {
@Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
if(beanClass.getSimpleName().equals(MyBeforeInstantiation.class.getSimpleName())){
try {
return new Constructor[]{beanClass.getDeclaredConstructor(PoJo.class,PoJo.class)};
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return null;
}
@Override
public int getOrder() {
return 0;
}
}
630

被折叠的 条评论
为什么被折叠?



