diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java index a912aa9a885..a8774fa7bed 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,10 +32,10 @@ public interface AdvisorChainFactory { * for the given advisor chain configuration. * @param config the AOP configuration in the form of an Advised object * @param method the proxied method - * @param targetClass the target class + * @param targetClass the target class (may be {@code null} to indicate a proxy without + * target object, in which case the method's declaring class is the next best option) * @return List of MethodInterceptors (may also include InterceptorAndDynamicMethodMatchers) */ - List getInterceptorsAndDynamicInterceptionAdvice( - Advised config, Method method, Class targetClass); + List getInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, Class targetClass); } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java index d99607cae14..740a7bb8ff2 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,19 +50,21 @@ public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializ public List getInterceptorsAndDynamicInterceptionAdvice( Advised config, Method method, Class targetClass) { - // This is somewhat tricky... we have to process introductions first, + // This is somewhat tricky... We have to process introductions first, // but we need to preserve order in the ultimate list. List interceptorList = new ArrayList(config.getAdvisors().length); - boolean hasIntroductions = hasMatchingIntroductions(config, targetClass); + Class actualClass = (targetClass != null ? targetClass : method.getDeclaringClass()); + boolean hasIntroductions = hasMatchingIntroductions(config, actualClass); AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance(); + for (Advisor advisor : config.getAdvisors()) { if (advisor instanceof PointcutAdvisor) { // Add it conditionally. PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor; - if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) { + if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) { MethodInterceptor[] interceptors = registry.getInterceptors(advisor); MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); - if (MethodMatchers.matches(mm, method, targetClass, hasIntroductions)) { + if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) { if (mm.isRuntime()) { // Creating a new object instance in the getInterceptors() method // isn't a problem as we normally cache created chains. @@ -78,7 +80,7 @@ public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializ } else if (advisor instanceof IntroductionAdvisor) { IntroductionAdvisor ia = (IntroductionAdvisor) advisor; - if (config.isPreFiltered() || ia.getClassFilter().matches(targetClass)) { + if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); } @@ -88,18 +90,19 @@ public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializ interceptorList.addAll(Arrays.asList(interceptors)); } } + return interceptorList; } /** * Determine whether the Advisors contain matching introductions. */ - private static boolean hasMatchingIntroductions(Advised config, Class targetClass) { + private static boolean hasMatchingIntroductions(Advised config, Class actualClass) { for (int i = 0; i < config.getAdvisors().length; i++) { Advisor advisor = config.getAdvisors()[i]; if (advisor instanceof IntroductionAdvisor) { IntroductionAdvisor ia = (IntroductionAdvisor) advisor; - if (ia.getClassFilter().matches(targetClass)) { + if (ia.getClassFilter().matches(actualClass)) { return true; } }