SpringAop when calling jdk dynamic proxy? When calling cglib

 

1. Import log4j.jar, open log4j DEBUG mode

 

2. Check the print log, you can find an important message:

2020-03-03 15:13:31,870 DEBUG [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator] - Creating implicit proxy for bean 'userService' with 0 common interceptors and 4 specific interceptors

spring when then perform aop, performed a method AnnotationAwareAspectJAutoProxyCreator class

 
3. By AnnotationAwareAspectJAutoProxyCreator is performed to find the ultimate in the parent class AbstractAutoProxyCreator Advisor protected [] buildAdvisors (the beanName String, Object [] specificInterceptors) Method  

 

4. Re in buildAdvisors  debug this method break point 

 

5.AbstractAutoProxyCreator  class were found to have a significant way createProxy follows:

protected Object createProxy(
    Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {

    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
    }

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.copyFrom(this);

    if (!proxyFactory.isProxyTargetClass()) {
        if (shouldProxyTargetClass(beanClass, beanName)) {
            proxyFactory.setProxyTargetClass(true);
        }
        else {
            evaluateProxyInterfaces(beanClass, proxyFactory);
        }
    }

    Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
    proxyFactory.addAdvisors(advisors);
    proxyFactory.setTargetSource(targetSource);
    customizeProxyFactory(proxyFactory);

    proxyFactory.setFrozen(this.freezeProxy);
    if (advisorsPreFiltered()) {
        proxyFactory.setPreFiltered(true);
    }
    // 核心代码
    return proxyFactory.getProxy(getProxyClassLoader());
}

 

The method then proceeds getProxy in ProxyFactory

public Object The getProxy (ClassLoader classLoader) {
     // createAopProxy determination method is selected jdk dynamic proxy mode or the proxy cglib 
    return createAopProxy () The getProxy (classLoader);. 
}

 

The method then proceeds to createAopProxy in ProxyCreatorSupport

protected  Final  the synchronized AOPProxy createAopProxy () {
     IF (! the this .active) { 
        the activate (); 
    } 
    // getAopProxyFactory () returns DefaultAopProxyFactory object, so the focus is on methods createAopProxy 
    return getAopProxyFactory () createAopProxy (. the this ); 
}

 

6. Go to createAopProxy DefaultAopProxyFactory class method, this method is how to choose JDK dynamic proxy or proxy Cglib

@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
    // 1.config.isProxyTargetClass() 代表 配置中的proxy-target-class属性true/false,默认false
    // 
    if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
        // 目标代理类,如 com.service.impl.UserServiceImpl
        Class<?> targetClass = config.getTargetClass();
        if (targetClass == null) {
            throw new AopConfigException("TargetSource cannot determine target class: " +
                                         "A target or the Either interface IS AN required for Proxy Creation.");
        } 
        // target class is an interface or if 
        IF (targetClass.isInterface () || Proxy.isProxyClass (targetClass)) {
             return  new new JdkDynamicAopProxy (config); 
        } 
        return  new new ObjenesisCglibAopProxy (config); 
    } 
    the else {
         return  new new JdkDynamicAopProxy (config ); 
    } 
}

 

The method is a AdvisedSupport type config parameters, AdvisedSupport ProxyConfig inherited class,

public  class of ProxyConfig the implements the Serializable {
     // Representative proxy-target-class configuration, if set to true, the use of proxy Cglib 
    Private  Boolean the proxyTargetClass = to false ; 

    Private  Boolean Optimize = to false ; 

    Boolean opaque = to false ; 

    Boolean the exposeProxy = to false ; 

    Private  Boolean Frozen = to false ; 

    // omit part of the code 
    
}

 

Guess you like

Origin www.cnblogs.com/caoxb/p/12406005.html