9

So I've got an aspect with a method declared with the following expression:

@Before("execution(* aaa.bbb.ccc.*.*(..))")

This works perfectly for all classes in the package aaa.bbb.ccc. Now, however, I would like to capture all classes in aaa.bbb, including those in aaa.bbb.ccc. So I tried backing it up to here:

@Before("execution(* aaa.bbb.*.*(..))")

This only grabs the classes from aaa.bbb, though, and ignores classes from aaa.bbb.ccc. Is there a way I can make the expression search for all subpackages recursively?

asteri
  • 11,402
  • 13
  • 60
  • 84

1 Answers1

23

Got it! The textual change is surprisingly trivial.

@Before("execution(* aaa.bbb.*.*(..))")

... becomes ...

@Before("execution(* aaa.bbb..*.*(..))")

Simply add the extra period between the package name and the qualifier, and you're off to the races.

One issue I encountered after making the change was that all of Spring blew up and crashed on me. That was because the aspect itself was in a subpackage of aaa.bbb. So if you do this, make sure you use a !within clause to exempt your aspect from trying to process itself.

asteri
  • 11,402
  • 13
  • 60
  • 84
  • 6
    This is basic stuff well covered in the documentation. Anyway, nice that you found it by yourself. You can optimise it by omitting one dot and one star: `execution(* aaa.bbb..*(..))` – kriegaex Jan 20 '15 at 22:34
  • @kriegaex Could you point me to the documentation where this is? I searched on Google for quite awhile before I came and asked here. – asteri Jan 20 '15 at 22:39
  • 2
    Look under "type name patterns" in the [Programming Guide](http://eclipse.org/aspectj/doc/released/progguide/printable.html#d0e5901). – kriegaex Jan 21 '15 at 12:27
  • You can also search for the string `..*` in order to find references in the [AspectJ 5 Developer's Notebook](http://eclipse.org/aspectj/doc/released/adk15notebook/printable.html), the [Development Environment Guide](http://eclipse.org/aspectj/doc/released/devguide/printable.html) and the [FAQ](http://eclipse.org/aspectj/doc/released/faq.html). I admit that most mentions are rather indirect, but the one from my previous comment is explicit, though maybe not easy to find. – kriegaex Jan 21 '15 at 12:28
  • 2
    The syntax pattern is also mentioned in the [Spring AOP documentation, section "Examples"](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-pointcuts-examples). – kriegaex Jan 21 '15 at 12:29