Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Potential fix for #185: Cache result of AopUtils.canApply
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantenney committed Jul 19, 2016
1 parent d9bbb29 commit c624df9
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;
}

}

1 comment on commit c624df9

@matlach
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, I'll test it as soon as possible.

Please sign in to comment.