spring aop 默认代理实现类为:DefaultAopProxyFactory
一下为部分代码片段:
/** Whether the CGLIB2 library is present on the classpath */
private static final boolean cglibAvailable =
ClassUtils.isPresent("net.sf.cglib.proxy.Enhancer", DefaultAopProxyFactory.class.getClassLoader());
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()) {
return new JdkDynamicAopProxy(config);
}
if (!cglibAvailable) {
throw new AopConfigException(
"Cannot proxy target class because CGLIB2 is not available. " +
"Add CGLIB to the class path or specify proxy interfaces.");
}
return CglibProxyFactory.createCglibProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
可以看出如果目标类是接口,则用jdk动态代理,否则判断classpath中是否引入cglib代理,没有则抛异常,有则用cglib代理
部分关于cglib代理的问题可以参考:http://netfork.iteye.com/blog/286215