@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: " +
"Either an interface or a target is required for proxy creation.");
}
// 目标类如果是一个接口 或者 (Proxy是JDK动态代理用到的一个类)也就是说这里表示目标类是否为这个Proxy 类型
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
这段代码是确定Spring选择JDK动态代理还理CGLIB代理的核心,
- config.isOptimize:表示是否使用了优化策略,配置的属性optimize值决定;
- config.isProxyTargetClass:表示是否是代理目标类,配置的属性proxy-target-class值决定;
- hasNoUserSuppliedProxyInterfaces:就是在判断代理的对象是否有实现接口
总结:
1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
从这个逻辑也可以间接看出spring推荐使用JDK动态代理,因为高版本的JDK中,至少JDK1.6以上JDK动态代理的效率远高于CGLIB(无论是单例模式还是多例模式)。但是如果代理的类没有相应的接口,此时只能使用CGLIB代理了,例如代理模式应用在POJO实体类上,或者是说你的JDK版本较低,这时你可以手动设置CGLIB代理。