如何配置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:
这是可行的,但是显然对设置方法或获取方法没有任何作用.
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*(..))")
这可以编译,但不会阻止捕获设置器/获取器:
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.
推荐答案
第二个不编译,因为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.
您的上一次尝试基本上是正确的,但仅忽略设置方法,而不忽略获取方法.试试这个:
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()
这样的布尔值的吸气剂,并且还想忽略它们,则必须将切入点扩展&& !execution(boolean is*())
.
带有名称模式的 - 这类切入点始终只是试探法,因此请提防方法名称,例如
getaway
,settleConflict
,isolate
.他们也将被忽略.
- 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屋!