我创建了简单的方面来计算执行特定方法的次数。我不得不说我是第一次这样做,所以它可能不是很漂亮。
首先,我创建了类似的东西:
@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。有人能告诉我为什么会发生这种情况以及如何解决吗?