Spring 5.x Source trip thirty-seven getBean processor extensions thirty

Figure no less

Here Insert Picture Description

applyMergedBeanDefinitionPostProcessors

After instantiation, the merger beandefinition, in fact, is to update beanthe definition of the matter, here again you can modify beanthe definition, there can only modify the RootBeanDefinitiontype. As I mentioned before, mainly InitDestroyAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessorand AutowiredAnnotationBeanPostProcessorprocessing methods have attributes or injection annotations. Respectively, to deal with the life cycle PostConstruct, PreDestroynotes,Resource,WebServiceRef,EJB注解 , Autowiredand Valuecomments.

applyBeanPostProcessorsAfterInitialization

In fact, this can be that with the above, such as the above that do some processing, you can use the back of this, we simply do the following subclasses look.
Here Insert Picture Description

Actual extension points

I want to define a comment, there is a property on the annotation, the number of times the method is to be printed, I hope to find a way to define all the attributes, and then print them.

MyAnnotation comment

Property countis the number printed.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    int count() default 0;
}

MyMergedBean

We comment some ways, to see if he will not be printed.

@Component
public class MyMergedBean {

    @MyAnnotation(count = 1)
    public void m1(String msg) {
        print(msg);
    }

    @MyAnnotation(count = 2)
    public void m2(String msg) {
        print(msg);
    }

    @MyAnnotation()
    public void m3(String msg) {
        print(msg);
    }

    private void print(String msg) {
        System.out.println(msg);
    }
}

MyMergedBeanDefinitionPostProcessor processor

This is mainly achieved postProcessMergedBeanDefinition, the processing done after instantiation, and then after the initialization postProcessBeforeInitializationspecific processing. The main print annotated methods, properties annotated according to.

@Component
public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {

    //bean名字对应的注解方法
    public Map<String,List<Method>> stringMethodMap;

    @Nullable
    private Class<? extends Annotation> myAnnotationType;

    public MyMergedBeanDefinitionPostProcessor(){
        myAnnotationType=MyAnnotation.class;
        stringMethodMap=new HashMap<>();

    }

    @Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        List<Method> list=new ArrayList<>();
        ReflectionUtils.doWithLocalMethods(beanType, method -> {
            if (this.myAnnotationType != null && method.isAnnotationPresent(this.myAnnotationType)) {
                list.add(method);
                stringMethodMap.put(beanName,list);
            }
        });
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(stringMethodMap.get(beanName)!=null){
            for (Method method : stringMethodMap.get(beanName)) {
                try {
                    MyAnnotation annotation = (MyAnnotation) method.getAnnotation(this.myAnnotationType);
                    for (int i = 0; i < annotation.count(); i++) {
                        method.invoke(bean,new Object[]{method.getName()});
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }

        return bean;
    }
}

Test category

   @Test
    public void MergedBeanDefinitionPostProcessorTest() throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();

    }

Here Insert Picture Description
I would like to change to change:
Here Insert Picture Description
Results:
Here Insert Picture Description
Why is not the order of definition enforce it, seems to be because JDKthe reflection is out of disorder, if you can order CGLIBthe ClassVisitorcome up, the specific springsource, there can refer to ConfigurationClassParserthe retrieveBeanMethodMetadata:
Here Insert Picture Description

Well, MyMergedBeanDefinitionPostProcessorin the end what use is it, to see your business matter, as long as you want to do after the extension is instantiated, you can try this, participate in beanthe process of initialization.

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