Pointcut matching methods with annotated parameters
I need to create an aspect with a pointcut matching a method if:
- Is public
- Its class is annotated with @Controller (Finally does not)
- One of its parameters (can have many) is annotated with @MyParamAnnotation.
I think the first two conditions are easy, but I don't know if its possible to accomplish the third with Spring. If it is not, maybe I can change it into:
- One of its parameters is an insta开发者_运维知识库nce of type com.me.MyType (or implements some interface)
Do you think it's possible to achieve this? And will performance be good?
Thanks
EDIT: One example of a matching method. As you can see, MyMethod is not annotated (but it can be).
@Controller
public class MyClass {
public void MyMethod (String arg0, @MyParamAnnotation Object arg1, Long arg3) {
...
}
}
EDIT: The solution I finally used, based on @Espen answers. As you can see, I changed my conditions a little: class doesn't actually need to be a @Controller.
@Around("execution(public * * (.., @SessionInject (*), ..))")
public void methodAround(JoinPoint joinPoint) throws Exception {
...
}
It was an interesting problem, so I created a little sample application to solve the case! (And improved it with Sinuhe's feedback afterwards.)
I have created a DemoController
class that should work as an example for the aspect:
@Controller
public class DemoController {
public void soSomething(String s, @MyParamAnnotation Double d, Integer i) {
}
public void doSomething(String s, long l, @MyParamAnnotation int i) {
}
public void doSomething(@MyParamAnnotation String s) {
}
public void doSomething(long l) {
}
}
The aspect that will add a join point on the first three methods, but not the last method where the parameter isn't annotated with @MyParamAnnotation
:
@Aspect
public class ParameterAspect {
@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void beanAnnotatedWithAtController() {
}
@Pointcut("execution(public * *(.., @aspects.MyParamAnnotation (*), ..))")
public void methodWithAnnotationOnAtLeastOneParameter() {
}
@Before("beanAnnotatedWithAtController() "
+ "&& methodWithAnnotationOnAtLeastOneParameter()")
public void beforeMethod() {
System.out.println("At least one of the parameters are "
+ "annotated with @MyParamAnnotation");
}
}
The first pointcut will create a joinpoint on all methods inside classes marked with @Controller
.
The second pointcut will add a joinpoint when the following conditions are met:
- public method
- first
*
is a wildcard for every return type. - second
*
is a wildcard for all methods in all classes. (..,
matches zero to many parameters of any type before the annotated parameter.@aspects.MyParamAnnotation (*),
matches a parameter annotated with the given annotation...)
matches zero to many parameters of any type after the annotated parameter.
Finally, the @Before
advice advises all methods where all conditions in both pointcuts are satisfied.
The pointcut works with both AspectJ and Spring AOP!
When it comes to performance. The overhead is small, especially with AspectJ that does the weaving on compile-time or load-time.
精彩评论