1 16 17 package org.springframework.aop.framework; 18 19 import org.springframework.aop.SpringProxy; 20 import org.springframework.util.ClassUtils; 21 22 47 public class DefaultAopProxyFactory implements AopProxyFactory { 48 49 50 private static final boolean cglibAvailable = 51 ClassUtils.isPresent("net.sf.cglib.proxy.Enhancer", DefaultAopProxyFactory.class.getClassLoader()); 52 53 54 public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { 55 if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { 56 Class targetClass = config.getTargetClass(); 57 if (targetClass == null) { 58 throw new AopConfigException("TargetSource cannot determine target class: " + 59 "Either an interface or a target is required for proxy creation."); 60 } 61 if (targetClass.isInterface()) { 62 return new JdkDynamicAopProxy(config); 63 } 64 if (!cglibAvailable) { 65 throw new AopConfigException( 66 "Cannot proxy target class because CGLIB2 is not available. " + 67 "Add CGLIB to the class path or specify proxy interfaces."); 68 } 69 return CglibProxyFactory.createCglibProxy(config); 70 } 71 else { 72 return new JdkDynamicAopProxy(config); 73 } 74 } 75 76 81 private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) { 82 Class [] interfaces = config.getProxiedInterfaces(); 83 return (interfaces.length == 0 || (interfaces.length == 1 && SpringProxy.class.equals(interfaces[0]))); 84 } 85 86 87 91 private static class CglibProxyFactory { 92 93 public static AopProxy createCglibProxy(AdvisedSupport advisedSupport) { 94 return new Cglib2AopProxy(advisedSupport); 95 } 96 } 97 98 } 99 | Popular Tags |