【发布时间】:2020-08-29 10:35:29
【问题描述】:
我创建了一个简单的方面来计算特定方法执行了多少次。我不得不说我是第一次这样做,所以它可能不是很漂亮。
首先,我创建了类似的东西:
@Aspect
@Component
public class ItemAspect {
private Map<String, Integer> functionsCalls = new HashMap<>();
public ItemAspect(){
this.functionsCalls.put("ItemApiController.addItem()", 0);
this.functionsCalls.put("ItemApiController.getItems()", 0);
}
@Pointcut("execution(* io.swagger.api.ItemApiController.getItems(..))")
private void itemApiControllerEx(){ }
@After("itemApiControllerEx()")
public void doAfterItemApiControllerEx (JoinPoint joinPoint) {
this.functionsCalls.put(joinPoint.getSignature().toString(), this.functionsCalls.get(joinPoint.getSignature().toString())+1);
}
public Map<String, Integer> getFunctionsCalls () {
return functionsCalls; }
}
它会计算正确执行“getItems()”的次数。 (是的,对于“addItem()”它也可以工作。) 但我想同时计算这两种方法,所以我将代码转换为:
@Aspect
@Component
public class ItemAspect {
private Map<String, Integer> functionsCalls = new HashMap<>();
public ItemAspect(){
this.functionsCalls.put("ItemApiController.addItem()", 0);
this.functionsCalls.put("ItemApiController.getItems()", 0);
}
@Pointcut("execution(* io.swagger.api.ItemApiController.*(..))")
private void itemApiControllerEx(){ }
@After("itemApiControllerEx()")
public void doAfterItemApiControllerEx (JoinPoint joinPoint) {
this.functionsCalls.put(joinPoint.getSignature().toString(), this.functionsCalls.get(joinPoint.getSignature().toString())+1);
}
public Map<String, Integer> getFunctionsCalls () {
return functionsCalls; }
}
现在,当我尝试获取签名时,我得到了 NullPointerException。有人能告诉我为什么会发生这种情况以及如何解决吗?
【问题讨论】: