如何统计一个Bean中的方法调用次数
典型回答 通过AOP即可实现,通过AOP对Bean进行代理,在每次执行方法前或者后进行几次计数统计。这个主要就是考虑好如何避免并发情况下不准,以及如何使用AOP实现代理。 主要的代码如下: 首先我们先自定义一个注解,有了这个注解之后,我们可以在想要统计的方法上加上这个注解: 1 2 3 4 5 6 7 8 9 10 11 12 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author Hollis **/ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodCallCount { } 接下来定义一个切面,来对这个注解进行增强处理: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Aspect @Component public class MethodCallCounterAspect { private Map<String, AtomicInteger> methodCallCountMap = new ConcurrentHashMap<>(); @Around("@annotation(com.hollis.chuang.java.bagu.MethodCallCount)") public Object facade(ProceedingJoinPoint pjp) throws Exception { Method method = ((MethodSignature) pjp.getSignature()).getMethod(); String methodName = method.getName(); try{ return pjp.proceed(); } catch (Throwable e) { //异常处理 } finally{ //计数+1 AtomicInteger counter = methodCallCountMap.computeIfAbsent(methodName, k -> new AtomicInteger(0)); counter.incrementAndGet(); } } public int getMethodCallCount(String methodName) { AtomicInteger counter = methodCallCountMap.get(methodName); return (counter != null) ? counter.get() : 0; } } 有了以上注解和切面后,只需要在我们想要统计的方法上使用该注解就行了: ...