2023-09-16  阅读(188)
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105168234

图不能少

202309162313277181.png

obtainFromSupplier实例提供器

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

202309162313292642.png

202309162313298693.png

实战

要把我们这个干净的类注册到容器里:

    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);
    
        }

202309162313303504.png

SmartInstantiationAwareBeanPostProcessor的determineCandidateConstructors

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

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

202309162313312276.png

实战

里面有两个构造方法的时候,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();
    
        }

结果输出:

202309162313319717.png

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;
        }
    }

结果:

202309162313324388.png
我们成功改变了原有的设置。

疑问

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

202309162313329509.png

2023091623133341410.png
这就解释了为什么我们的在他们前面了,本来是他们先注册进去的,只最后被重新注册到后面了,所以可以先执行我们的处理器,返回的构造器不为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;
        }
    }

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

阅读全文
  • 点赞