Spring Boot 实例化bean如何选择代理方式
作者:子瞻
这篇文章主要为大家介绍了Spring Boot实例化bean如何选择代理方式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
Spring Boot 实例化bean如何选择代理方式

图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动态代理;
以上就是Spring Boot 实例化bean如何选择代理方式的详细内容,更多关于SpringBoot实例化bean代理的资料请关注脚本之家其它相关文章!
