4、寻找bean所匹配的advisors

2017-08-01  本文已影响29人  Britney_z

protected List findEligibleAdvisors(Class beanClass, String beanName) {

List candidateAdvisors = findCandidateAdvisors();

List eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);

extendAdvisors(eligibleAdvisors);

if (!eligibleAdvisors.isEmpty()) {

eligibleAdvisors = sortAdvisors(eligibleAdvisors);

}

return eligibleAdvisors;

}

  findAdvisorsThatCanApply方法寻找bean所匹配的增强器。

protected List findAdvisorsThatCanApply(List candidateAdvisors, Class beanClass, String beanName) {

ProxyCreationContext.setCurrentProxiedBeanName(beanName);

try {

return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);

}

finally {

ProxyCreationContext.setCurrentProxiedBeanName(null);

}

}

AopUtilsfindAdvisors.ThatCanApply方法具体实现

public static List findAdvisorsThatCanApply(List candidateAdvisors, Class clazz) {

if (candidateAdvisors.isEmpty()) {

return candidateAdvisors;

}

List eligibleAdvisors = new LinkedList();

for (Advisor candidate : candidateAdvisors) {

//IntroductionAdvisor单独处理

if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {

eligibleAdvisors.add(candidate);

}

}

boolean hasIntroductions = !eligibleAdvisors.isEmpty();

for (Advisor candidate : candidateAdvisors) {

if (candidate instanceof IntroductionAdvisor) {

// already processed

continue;

}

if (canApply(candidate, clazz, hasIntroductions)) {

eligibleAdvisors.add(candidate);

}

}

return eligibleAdvisors;

}

主要看canApply方法,IntroductionAdvisor和PointcutAdvisor也是分开处理,咱们主要看PointcutAdvisor

public static boolean canApply(Advisor advisor, Class targetClass, boolean hasIntroductions) {

if (advisor instanceof IntroductionAdvisor) {

return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);

}

else if (advisor instanceof PointcutAdvisor) {

PointcutAdvisor pca = (PointcutAdvisor) advisor;

return canApply(pca.getPointcut(), targetClass, hasIntroductions);

}

else {

// It doesn't have a pointcut so we assume it applies.

return true;

}

}

canApply方法

public static boolean canApply(Pointcut pc, ClasstargetClass, boolean hasIntroductions) {

Assert.notNull(pc, "Pointcut must not be null");

if (!pc.getClassFilter().matches(targetClass)) {

return false;

}MethodMatcher methodMatcher = pc.getMethodMatcher();

if (methodMatcher == MethodMatcher.TRUE) {// No need to iterate the methods if we're matching any method anyway...return true;

}

IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;

if (methodMatcher instanceof IntroductionAwareMethodMatcher) {introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;}

Set> classes = new LinkedHashSet>(ClassUtils.getAllInterfacesForClassAsSet(targetClass));

classes.add(targetClass);

for (Class clazz : classes) {

Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz);

for (Method method : methods) {

if ((introductionAwareMethodMatcher != null &&

introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) ||

methodMatcher.matches(method, targetClass)) {

return true;

}

}

}

return false;

}


这里主要遍历目标类所有方法,看是否有匹配。

上一篇 下一篇

猜你喜欢

热点阅读