If we want to intercept methods with dynamic pointcut, we need to override matches and getClassFilter methods of DynamicMethodMatcher abstract class. The dynamic pointcut can verify the arguments of the method at runtime. This is a difference with static pointcut.
In above dynamic pointcut class, we verify the class name must be MyLogicClass and the value of the first argument in method must larger than 10, otherwise the method will not be intercepted.
Create our MyDynamicPointcut bean and MyAroundAdvice bean and put them into DefaultPointcutAdvisor bean. Finally put the advisor and target class to the ProxyFactoryBean.
We call the methodTwo twice with different arguments. Let’s see the output.
Dynamic check for methodOne
MethodOne is Running
--------------------
Dynamic check for methodTwo
MethodTwo is running with argument 1
--------------------
Dynamic check for methodTwo
>>>>>> Invoking method methodTwo
MethodTwo is running with argument 11
>>>>>> Invoked Done
You see, only methodTwo with argument 11 (larger than 10) is intercepted.