2

What's up? folks!

I'm trying to intercept all classes that contains a specific word in their names... something as below:

@Before("execution(* com.domain.model.*.*Repository.save(..))")

I have the following methods to intercept:

com.domain.model.user.UserRepository.save(User user);
com.domain.model.xpto.XPTORepository.save(XPTO xpto);
com.domain.model.foo.FooRepository.save(Foo foo);

I have tried this: (worked, but looks horrible)

@Before("execution(* *.save(..)) && within(com.domain.model..*)")
public void validateBeforeSave(final JoinPoint jp) throws Throwable {
    if (jp.getSignature().toString().contains("Repository.")) {
        ...
    }
}

Thanks!!!

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Gustavo Amaro
  • 131
  • 1
  • 4
  • 7

1 Answers1

0

What is the problem with your own suggestion?

execution(* com.domain.model.*.*Repository.save(..))

should work nicely with the sample package + class names you provided. If is does not work your real package names are different, e.g. you have more than one subpackage below model. In this case you can make the pointcut more generic using the .. construct which is also used in your ugly workaround:

execution(* com.domain.model..*Repository.save(..))

Or maybe your *Repository classes all inherit from a common superclass or implement the same interface, e.g. interface com.domain.model.Repository or abstract class com.domain.model.BaseRepository. In this case you could do without the string matching and just use something like

execution(* com.domain.model.Repository+.save(..))

or

execution(* com.domain.model.BaseRepository+.save(..))

The + means "this class and all of its subclasses".

kriegaex
  • 63,017
  • 15
  • 111
  • 202