Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化 spring 内置代理机制和 matrix代理机制组合使用的情况 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -55,8 +53,8 @@ public abstract class AbstractAutoScanProxy extends AbstractAutoProxyCreator {
System.out.println("");
}

// Bean名称和Bean对象关联
private final Map<String, Object> beanMap = new HashMap<String, Object>();
// Bean名称和当前Bean对象关联
private final Map<String, Object> currentBeanMap = new HashMap<String, Object>();

// Spring容器中哪些接口或者类需要被代理
private final Map<String, Boolean> proxyMap = new HashMap<String, Boolean>();
Expand Down Expand Up @@ -154,6 +152,13 @@ private String[] convertInterceptorNames(Class<? extends MethodInterceptor>[] 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();
Expand All @@ -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()) {
Expand Down Expand Up @@ -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) {
// 设置不同场景下的接口代理,还是类代理
Expand Down