Skip to content

Commit

Permalink
Potential fix for ryantenney#185: Cache result of AopUtils.canApply
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantenney authored and Marcel Vingerling committed Sep 1, 2016
1 parent f60303c commit 848d87f
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.ryantenney.metrics.spring;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.aopalliance.aop.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,6 +42,8 @@ class AdvisingBeanPostProcessor implements BeanPostProcessor {
private final AdviceFactory adviceFactory;
private final ProxyConfig proxyConfig;

private final ConcurrentMap<Class<?>, Boolean> applicabilityCache = new ConcurrentHashMap<Class<?>, Boolean>(256);

public AdvisingBeanPostProcessor(final Pointcut pointcut, final AdviceFactory adviceFactory, final ProxyConfig proxyConfig) {
this.pointcut = pointcut;
this.adviceFactory = adviceFactory;
Expand All @@ -58,7 +63,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {

final Class<?> targetClass = AopUtils.getTargetClass(bean);

if (AopUtils.canApply(pointcut, targetClass)) {
if (canApply(targetClass)) {
final Advice advice = adviceFactory.getAdvice(bean, targetClass);
final Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);

Expand Down Expand Up @@ -87,4 +92,15 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}

private boolean canApply(final Class<?> targetClass) {
Boolean cachedApplicability = applicabilityCache.get(targetClass);
if (cachedApplicability != null) {
return (boolean) cachedApplicability;
}

boolean canApply = AopUtils.canApply(pointcut, targetClass);
applicabilityCache.putIfAbsent(targetClass, canApply);
return canApply;
}

}

0 comments on commit 848d87f

Please sign in to comment.