Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It was an interesting problem, so I created a little sample application to solve the case! (And improved it with Sinuhe's feedback afterwards.)</p> <p>I have created a <code>DemoController</code> class that should work as an example for the aspect: </p> <pre><code>@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) { } } </code></pre> <p>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 <code>@MyParamAnnotation</code>:</p> <pre><code>@Aspect public class ParameterAspect { @Pointcut("within(@org.springframework.stereotype.Controller *)") public void beanAnnotatedWithAtController() { } @Pointcut("execution(public * *(.., @aspects.MyParamAnnotation (*), ..))") public void methodWithAnnotationOnAtLeastOneParameter() { } @Before("beanAnnotatedWithAtController() " + "&amp;&amp; methodWithAnnotationOnAtLeastOneParameter()") public void beforeMethod() { System.out.println("At least one of the parameters are " + "annotated with @MyParamAnnotation"); } } </code></pre> <p>The first pointcut will create a joinpoint on all methods inside classes marked with <code>@Controller</code>.</p> <p>The second pointcut will add a joinpoint when the following conditions are met:</p> <ul> <li>public method</li> <li>first <code>*</code> is a wildcard for every return type.</li> <li>second <code>*</code> is a wildcard for all methods in all classes.</li> <li><code>(..,</code> matches zero to many parameters of any type before the annotated parameter.</li> <li><code>@aspects.MyParamAnnotation (*),</code> matches a parameter annotated with the given annotation.</li> <li><code>..)</code> matches zero to many parameters of any type after the annotated parameter.</li> </ul> <p>Finally, the <code>@Before</code> advice advises all methods where all conditions in both pointcuts are satisfied.</p> <p>The pointcut works with both AspectJ and Spring AOP!</p> <p>When it comes to performance. The overhead is small, especially with AspectJ that does the weaving on compile-time or load-time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload