I am learning spring AOP and got the following pointcut expression from one of the examples in AOP section of spring-framework-reference: Proceeding with Arguments
@Around("execution(List<Account> find*(..)) && " +
"com.xyz.myapp.SystemArchitecture.inDataAccessLayer() && " +
"args(accountHolderNamePattern)")
public Object preProcessQueryPattern(ProceedingJoinPoint pjp,
String accountHolderNamePattern) throws Throwable {
String newPattern = preProcess(accountHolderNamePattern);
return pjp.proceed(new Object[] {newPattern});
}
As per my understanding this pointcut expression is incorrect as the and condition would never satisfy. Am I missing something very obvious?
Also I attempted the following (ref):
@Component
public class TestBean {
public void testMethod() {
}
}
Aspect class
@Before("com.test.bean.TestBean.testMethod()")
public void testAspect() {
System.out.println("Worked");
}
which failed with
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut testMethod
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:319)
There are syntax errors in both pointcuts. You cannot just write a method signature without execution()
around it if you want to intercept a method. Also please do not forget to specify a return type or at least *
for any return type.
The first pointcut would be syntactically correct like this:
@Around(
"execution(List<Account> find*(..)) && " +
"execution(* com.xyz.myapp.SystemArchitecture.inDataAccessLayer()) && " +
"args(accountHolderNamePattern)"
)
But It would still not match any method because of &&
(logical AND) requires both method executions to be true at the same time, which is impossible. Either find*(..)
or inDataAccessLayer()
is being executed, never both at the same time. You could work with ||
(logical OR) instead, but then the args()
matcher would be ambiguous and thus the pointcut would be invalid again. I cannot tell you how to fix your pointcut unless you explain to me what you want to achieve. The way it is now it does not make any sense.
As for the second pointcut, you can fix it like this:
@Before("execution(* com.test.bean.TestBean.testMethod())")
The error message you quoted means that the AspectJ parser thinks you are referring to a named pointcut instead of your intended method execution.