关于springboot:Spring-Boot-实例化bean如何选择代理方式

39次阅读

共计 879 个字符,预计需要花费 3 分钟才能阅读完成。

图 1
咱们再回顾一下之前的事务源码剖析有提到,执行到 AbstractAutowireCapableBeanFactory.initializeBean()->applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName)->AbstractAutoProxyCreator.postProcessAfterInitialization()->AbstractAutoProxyCreator.wrapIfNecessary()->DefaultAopProxyFactory.createAopProxy() 链条创立代理;

public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {Class<?> targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class:" +
                        "Either an interface or a target is required for proxy creation.");
            }
            // 如果是接口
            if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {return new JdkDynamicAopProxy(config);
            }
            // 如果不是接口
            return new ObjenesisCglibAopProxy(config);
        }
        else {return new JdkDynamicAopProxy(config);
        }
    }

通过下面的形式判断 cglib 还是 jdk 动静代理;

正文完
 0