diff --git a/matrix-aop/src/main/java/com/nepxion/matrix/proxy/aop/AbstractAutoScanProxy.java b/matrix-aop/src/main/java/com/nepxion/matrix/proxy/aop/AbstractAutoScanProxy.java index c40a22c..968af53 100644 --- a/matrix-aop/src/main/java/com/nepxion/matrix/proxy/aop/AbstractAutoScanProxy.java +++ b/matrix-aop/src/main/java/com/nepxion/matrix/proxy/aop/AbstractAutoScanProxy.java @@ -9,11 +9,9 @@ * @version 1.0 */ -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - +import com.nepxion.matrix.proxy.mode.ProxyMode; +import com.nepxion.matrix.proxy.mode.ScanMode; +import com.nepxion.matrix.proxy.util.ProxyUtil; import org.aopalliance.intercept.MethodInterceptor; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; @@ -22,12 +20,12 @@ import org.springframework.aop.TargetSource; import org.springframework.aop.framework.AopProxyUtils; import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; -import org.springframework.beans.BeansException; import org.springframework.stereotype.Component; -import com.nepxion.matrix.proxy.mode.ProxyMode; -import com.nepxion.matrix.proxy.mode.ScanMode; -import com.nepxion.matrix.proxy.util.ProxyUtil; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; public abstract class AbstractAutoScanProxy extends AbstractAutoProxyCreator { private static final long serialVersionUID = 6827218905375993727L; @@ -55,8 +53,8 @@ public abstract class AbstractAutoScanProxy extends AbstractAutoProxyCreator { System.out.println(""); } - // Bean名称和Bean对象关联 - private final Map beanMap = new HashMap(); + // Bean名称和当前Bean对象关联 + private final Map currentBeanMap = new HashMap(); // Spring容器中哪些接口或者类需要被代理 private final Map proxyMap = new HashMap(); @@ -154,6 +152,13 @@ private String[] convertInterceptorNames(Class[] co return null; } + @Override + protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { + //存储当前实例化过程中的当前状态下的bean,有可能是被代理过的bean + currentBeanMap.put(beanName, bean); + return super.wrapIfNecessary(bean, beanName, cacheKey); + } + @Override protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) { boolean scanPackagesEnabled = scanPackagesEnabled(); @@ -166,16 +171,11 @@ protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanN } } - // 根据Bean名称获取Bean对象 - Object bean = beanMap.get(beanName); + // 根据Bean名称获取当前Bean对象 + Object bean = currentBeanMap.get(beanName); // 获取最终目标类 - Class targetClass = null; - if (bean != null /* && AopUtils.isCglibProxy(bean) */) { - targetClass = AopProxyUtils.ultimateTargetClass(bean); - } else { - targetClass = beanClass; - } + Class targetClass = AopProxyUtils.ultimateTargetClass(bean); // Spring容器扫描实现类 if (!targetClass.isInterface()) { @@ -319,26 +319,6 @@ protected Object[] scanAndProxyForMethod(Class targetClass, String targetClas return proxied ? interceptors : DO_NOT_PROXY; } - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - Object object = super.postProcessBeforeInitialization(bean, beanName); - - // 前置扫描,把Bean名称和Bean对象关联存入Map - boolean scanPackagesEnabled = scanPackagesEnabled(); - if (scanPackagesEnabled) { - boolean scanPackagesContained = scanPackagesContained(bean.getClass()); - if (scanPackagesContained) { - // 如果beanClass的类路径,包含在扫描目录中,则加入beanMap - beanMap.put(beanName, bean); - } - } else { - // scanPackagesEnabled=false,表示“只扫描指定目录”的方式未开启,则所有扫描到的bean都加入beanMap - beanMap.put(beanName, bean); - } - - return object; - } - @Override protected boolean shouldProxyTargetClass(Class beanClass, String beanName) { // 设置不同场景下的接口代理,还是类代理