Is there an AspectJ pointcut expression that searches all subpackages?
Asked Answered
G

1

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?

Gaziantep answered 20/1, 2015 at 19:44 Comment(0)
G
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.

Gaziantep answered 20/1, 2015 at 20:9 Comment(5)
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..*(..))Grundy
@Grundy Could you point me to the documentation where this is? I searched on Google for quite awhile before I came and asked here.Gaziantep
Look under "type name patterns" in the Programming Guide.Grundy
You can also search for the string ..* in order to find references in the AspectJ 5 Developer's Notebook, the Development Environment Guide and the FAQ. I admit that most mentions are rather indirect, but the one from my previous comment is explicit, though maybe not easy to find.Grundy
The syntax pattern is also mentioned in the Spring AOP documentation, section "Examples".Grundy

© 2022 - 2024 — McMap. All rights reserved.