Phate
Phate

Reputation: 6622

Exclude architectural services from being processed by aspect

I'm creating an aspect to wrap my services but I'm also defining some services under the package

com.foo.arch

My application classes will be instead under

com.foo

Please notice that arch is a subpackage of com.foo but I can't put the "arch" keyword before.Isn't there a pointcut expression to say:

All classess under com.foo not having "arch" as third package?

Upvotes: 0

Views: 45

Answers (2)

kriegaex
kriegaex

Reputation: 67477

How about adding this to your pointcut?

within(com.foo..*) && !within(com.foo.arch..*)

This will capture all sub-packages of com.foo except for com.foo.arch and its respective sub-packages.

If you only want com.foo and no sub-packages at all, it would simply be:

within(com.foo.*)

Upvotes: 1

R.G
R.G

Reputation: 7141

Consider an interface SampleService

  public interface SampleService {
        void test();
    }

and has 3 implementations in these three package

com

com.foo

com.foo.arch

an aspect to exclude arch package and only advice classes under com and com.foo would be as follows

@Component
@Aspect
public class ExcludeAspect {

    @Around("execution(* test()) && !within(com.foo.arch.*)")
    public void adviceTestMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Adviced");
        pjp.proceed();
    }
}

Reference : within

Combining Pointcut Expressions

Hope this helps

Upvotes: 1

Related Questions