AbstractAdvisorAutoProxyCreator Springboot source code analysis of the

Summary:

SpringAgent in the upper layer is divided into ProxyCreatorSupportand ProxyProcessorSupportthe former is based on proxy factory, which is based on the post-processor, it can be considered that automatic post agent. When springthe container needs to aopbe woven into a beantime more, simply uses ProxyFacotryBeanwill undoubtedly add a lot of workload (because each Bean! Had to write a manual). So automatic proxy will play its role in the.

file

Spring automatically create proxy Category

In the interior, Spring use BeanPostProcessorto make automatic generation of proxy. Class-based implementation of BeanPostProcessor autoproxy creator of the vessel in accordance with some examples of rules Beangenerated for the matching agent instance when the Bean. The proxy creator can be divided into three categories:

  • Bean automatic proxy configuration name generated based on rules: allows automatic creator create proxy agent instance for a particular configuration group Bean name, class implementedBeanNameAutoProxyCreator
  • Based on Advisorits Advisor on all containers are scanned automatically create matching proxy mechanism, which automatically cut applied to the matching Bean, the implementation class is DefaultAdvisorAutoProxyCreator(it also supports prefix matching)
  • Based on the Bean AspectJannotated automatic proxy generator: create Bean cut the AspectJ annotation contains automatic proxy instance, implementation class is AnnotationAwareAspectJAutoProxyCreator, it is our @EnableAspectJAutoProxyimport, which is our current most widely used way ~

BeanNameAutoProxyCreator

    package com.github.dqqzj.springboot.aop;
    
    import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    
    /**
     * @author qinzhongjian
     * @date created in 2019-08-25 09:43
     * @description: TODO
     * @since JDK 1.8.0_212-b10
     */
    @Component
    public class MyBeanNameAutoProxyCreator extends BeanNameAutoProxyCreator {
        @PostConstruct
        public void init() {
            super.setBeanNames("aopService", "abstractAutoProxyCreatorService");
            super.setInterceptorNames("myMethodBeforeAdvice");
        }
    
    }

file

If you want to use their registered @Beanplace of @EnableAspectJAutoProxydefault to automatically register your creator AnnotationAwareAspectJAutoProxyCreator, you can sign up to a Bean Bean name as follows:

    // 手动注册一个自动代理创建器,且名字务必叫AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME
    @Bean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME) 
    public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
        ...
    }

AbstractAutoProxyCreator

    public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
            implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
            ...
    }

AbstractAutoProxyCreatorIt is an abstract realization of the auto proxy creator. Most importantly, it implements the SmartInstantiationAwareBeanPostProcessorinterface, will be involved in the Spring IoCcontainer Bean instantiation process, AbstractAutowireCapableBeanFactorythere is a piece of code like this

    try {
                // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
                Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
                if (bean != null) {
                    return bean;
                }
            }

But generally it will not take effect, because this resolveBeforeInstantiationis only for self-definition targetsource, because the custom targetsourceis not so sure spring of bean does not need to follow a series of examples of initialization. It can be done directly in resolveBeforeInstantiation proxy. Simple Well, this code can be ignored, developers generally use less.

How to make resolveBeforeInstantiation return directly bean?

    package com.github.dqqzj.springboot.aop;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
    
    /**
     * @author qinzhongjian
     * @date created in 2019-08-25 11:35
     * @description: TODO
     * @since JDK 1.8.0_212-b10
     */
    public class AopServiceInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
        @Override
        public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
            if (beanClass.isInstance(AopService.class)) {
                return new AopService();
            }
            return null;
        }
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }
    }

file

This is the springfirst time the post-processor, if so directly returned, out of the equivalent of IOCthe life cycle of the same dependency injection, filling and other properties which are not treated, it must be noted that when using, better not to use this feature.

In the process of initializing the bean in the follow-up there are two particularly important post-treatment process, the impact of asynchronous cycle of dependence and even annotations and notes things have more or less, follow-up will continue to analyze them.

summary:

SpringAOPShould be avoided to create their own AutoProxyCreator, internal mechanisms and their complexity will inevitably arise other unusual problems because the problem is not thought to share the above content rarely, because I feel familiar with this most crucial architects, various subclasses While achieve very different, but all you want to keep in mind or are familiar with it is not realistic, only familiar with his design in order to meet the problem can be easily solved turn source.

Guess you like

Origin www.cnblogs.com/qinzj/p/11407493.html