I need to create an aspect with a pointcut matching a method if:
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:
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 {
...
}
This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")
You can use Spring AOP, create point cut using @Around . Then you can use the below code to change the arguments of the method, based on the condition. int index = 0; Object[] modifiedArgs = proceedingJoinPoint.
AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)
Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.
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:
*
is a wildcard for every return type.*
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With