From c624df9e6997ddc4cd1e5de0bf09938191ce0b39 Mon Sep 17 00:00:00 2001 From: Ryan Tenney Date: Tue, 19 Jul 2016 12:20:45 -0400 Subject: [PATCH] Potential fix for #185: Cache result of AopUtils.canApply --- .../spring/AdvisingBeanPostProcessor.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ryantenney/metrics/spring/AdvisingBeanPostProcessor.java b/src/main/java/com/ryantenney/metrics/spring/AdvisingBeanPostProcessor.java index 10085ce7..f32583ee 100644 --- a/src/main/java/com/ryantenney/metrics/spring/AdvisingBeanPostProcessor.java +++ b/src/main/java/com/ryantenney/metrics/spring/AdvisingBeanPostProcessor.java @@ -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; @@ -39,6 +42,8 @@ class AdvisingBeanPostProcessor implements BeanPostProcessor { private final AdviceFactory adviceFactory; private final ProxyConfig proxyConfig; + private final ConcurrentMap, Boolean> applicabilityCache = new ConcurrentHashMap, Boolean>(256); + public AdvisingBeanPostProcessor(final Pointcut pointcut, final AdviceFactory adviceFactory, final ProxyConfig proxyConfig) { this.pointcut = pointcut; this.adviceFactory = adviceFactory; @@ -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); @@ -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; + } + }