Spring 5.x 源码之旅三十六之实例提供器和选择构造函数扩展点

图不能少

在这里插入图片描述

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中了,而AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor刚好是这个类型的,他们最后又被注册了一遍,而注册的方法是会把存在的删除,然后把新注册的放最后。
在这里插入图片描述
在这里插入图片描述
这就解释了为什么我们的在他们前面了,本来是他们先注册进去的,只最后被重新注册到后面了,所以可以先执行我们的处理器,返回的构造器不为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;
    }
}

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值