package org.springframework.aop.framework;
import java.io.Serializable;import java.lang.reflect.Proxy; import org.springframework.aop.SpringProxy; @SuppressWarnings("serial")public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { //动态代理方法 @Override 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); } } private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) { Class<?>[] ifcs = config.getProxiedInterfaces(); return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0]))); } }
config.isOptimize()-------------------------------------------------是否进行优化,默认是falseconfig.isProxyTargetClass()-----------------------------------------是否强制使用cglib代理。但它为true也不是肯定就采用cglib,因为下面还有一个判断条件,即目标类是接口,则使用jdk动态代理的方式。hasNoUserSuppliedProxyInterfaces(config)----------------------------目标类没有实现接口,或者有但是是接口类型是SpringProxy 当config.isOptimize(),config.isProxyTargetClass(),hasNoUserSuppliedProxyInterfaces(config)全部为false时候使用JDK动态代理JdkDynamicAopProxy
当config.isOptimize(),config.isProxyTargetClass(),hasNoUserSuppliedProxyInterfaces(config)有一个为true,且代理对象不为接口时或者配置注解
@EnableAspectJAutoProxy(proxyTargetClass = true),使用cglib动态代理
原文地址:https://www.cnblogs.com/WQLLL/p/11073388.html
时间: 2024-10-18 14:30:33