简体   繁体   English

如何配置AspectJ忽略getter和setter

[英]How to configure aspectj ignore getters and setters

I have an aspect that currently works to capture all public method executions within my package. 我有一个方面目前可以捕获包中的所有公共方法执行。

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..*)")

This does not compile: 这不会编译:

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

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. 尝试编译方面时出现编译错误。

How can I exclude getters and setters in aspectJ? 如何在AspectJ中排除获取器和设置器?

The second one does not compile because within() needs a type signature, not a method signature. 第二个不编译,因为within()需要类型签名,而不是方法签名。 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. 我只是写了一个新书 ,以纠正那些虚假信息。

Your last try is basically right, but only ignores setters, not getters. 您的最后一次尝试基本上是正确的,但仅忽略设置方法,而不忽略获取方法。 Try this: 尝试这个:

Driver application: 驱动程序应用程序:

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());
  }
}

Aspect: 方面:

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();
  }
}

Console log: 控制台日志:

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

Please note that 请注意

  • 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*()) . 如果您有类似isActive()这样的布尔值的吸气剂,并且也想忽略它们,则必须通过&& !execution(boolean is*())扩展切入点。
  • those kinds of pointcuts with name patterns are always just heuristics, so beware of method names such as getaway , settleConflict , isolate . 带有名称模式的切入点始终只是启发式的,因此请提防方法名称,例如getawaysettleConflictisolate They would also be ignored. 他们也将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM