2
votes

I'm trying to implement a logger aspect that can be applied across multiple packages in my application. It is a big spring web application having many modules. Each module has it's own controllers, services and DAOs. The pointcuts I define targets all the public methods on each classes within the packages. It's public * com.abc.module1.controllers.*.*(..) for the controllers, public * com.abc.module1.services.*.*(..) for the services and public * com.abc.module1.daos.*.*(..) for the DAOs in module1. I've around 30 such modules. So the problem is, to cover all these modules I have to define pointcuts for each of them. All the modules follow the same package structure. Is there a way I can specify the the pointcut that can cover all controllers, services that are placed in different modules? Does spring AOP allows wild cards in package names?

1

1 Answers

4
votes

I do recommend reading the AspectJ documentation, e.g.

Then you would not need to ask a question like this here.

Anyway, here is how you solve your problem (I am adding line breaks for better readability):

execution(public * com.abc..controllers..*(..)) ||
execution(public * com.abc..services..*(..)) ||
execution(public * com.abc..daos..*(..))

Or alternatively:

(
  within(com.abc..controllers..*) ||
  within(com.abc..services..*) ||
  within(com.abc..daos..*)
) &&
execution(public * *(..))

If you use Spring AOP instead of full-fledged AspectJ you can make it even simpler because Spring AOP basically only knows execution() and no other pointcuts like call(), constructor execution, set() / get() etc.

within(com.abc..controllers..*) ||
within(com.abc..services..*) ||
within(com.abc..daos..*)

With JDK dynamic proxies (default when your components implement interfaces) this would only target public method calls anyway. With CGLIB proxies it would also target protected and package-private ones. BTW, you also need to upgrade your simple pointcut to one of the above, more explicit ones, if you ever migrate from Spring AOP to AspectJ later.