如何配置aspectj忽略getter和setter [英] How to configure aspectj ignore getters and setters

查看:34
本文介绍了如何配置aspectj忽略getter和setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方面目前可以捕获我的包中的所有公共方法执行.

I have an aspect that currently works to capture all public method executions within my package.

我想修改它以排除 setter 和 getter,所以我尝试了,这些是我尝试过的变体:

I would like to modify that to exclude both setters and getters, so I tried that and these are the variants I tried:

这个有效,但显然对 setter 或 getter 没有任何作用.

This one works, but does obviously does not do anything for setters or getters.

@Around("execution(public * *(..)) && !within(com.walterjwhite.logging..*)")

这不会编译:

@Around("execution(public * *(..)) && !within(* set*(..))")

这会编译,但不会阻止捕获 setter/getter:

This compiles, but doesn't prevent capturing setters/getters:

@Around("execution(public * *(..)) && !execution(* set*(..))")

我也看到这篇文章作为参考,但没有用.尝试编译方面时出现编译错误.

I also came across this post as a reference, but that didn't work. I get compilation errors when trying to compile the aspect.

如何在aspectJ中排除getter和setter?

推荐答案

第二个无法编译,因为 within() 需要类型签名,而不是方法签名.您所指的答案完全错误,我不知道为什么它被接受.我刚刚写了一个新的以纠正错误信息.

The second one does not compile because within() needs a type signature, not a method signature. The answer you are referring to is just plain wrong, I have no idea why it was accepted. I just wrote a new one in order to correct that false information.

你的最后一次尝试基本正确,但只忽略了 setter,而不是 getter.试试这个:

Your last try is basically right, but only ignores setters, not getters. Try this:

驱动程序应用:

package de.scrum_master.app;

public class Application {
  private int id;
  private String name;

  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void doSomething() {
    System.out.println("Doing something");
  }

  public static void main(String[] args) {
    Application application = new Application();
    application.setId(11);
    application.setName("John Doe");
    application.doSomething();
    System.out.println(application.getId() + " - " + application.getName());
  }
}

方面:

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class MyAspect {
  @Around("execution(public * *(..)) && !execution(void set*(*)) && !execution(!void get*())")
  public Object myAdvice(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    System.out.println(thisJoinPoint);
    return thisJoinPoint.proceed();
  }
}

控制台日志:

execution(void de.scrum_master.app.Application.main(String[]))
execution(void de.scrum_master.app.Application.doSomething())
Doing something
11 - John Doe

请注意

  • 如果你有像 isActive() 这样的布尔值的 getter 并且也想忽略它们,你必须通过 && 扩展切入点.!execution(boolean is*()).
  • 那些带有名称模式的切入点总是试探性的,所以要注意诸如getawaysettleConflictisolate 之类的方法名称.它们也会被忽略.
  • if you have getters for boolean values like isActive() and also want to ignore them, you have to extend the pointcut by && !execution(boolean is*()).
  • those kinds of pointcuts with name patterns are always just heuristics, so beware of method names such as getaway, settleConflict, isolate. They would also be ignored.

这篇关于如何配置aspectj忽略getter和setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆