Search code examples
springaopspring-aopaspect

Not Matching this type name


I have many aspect class in com.aop.aspect package. What I want to do is to work all class except for one class named for com.aop.dao.MyDemoLoggingAspect

When I run the app, there is an error appeared on the console.

java.lang.IllegalArgumentException: warning no match for this type name: com.aop.dao.MyDemoLoggingAspect [Xlint:invalidAbsoluteTypeName]

Here is my aspect class

@Aspect
public class LuvAopExpressionsOrder {

    @Pointcut("execution(* com.aop.dao.*.*(..))")
    public void forDaoPackage() {}

    // create pointcut for getter methods
    @Pointcut("execution(* com.aop.dao.*.get*(..))")
    public void getter() {}

    // create pointcut for setter methods
    @Pointcut("execution(* com.aop.dao.*.set*(..))")
    public void setter() {}

    // create pointcut for setter methods
    @Pointcut("!execution(* com.aop.dao.MyDemoLoggingAspect.*(..))")
    public void excludeMyDemoLoggingAspect() {}

    // create pointcut: include package ... exclude getter/setter and MyDemoLoggingAspect
    @Pointcut("forDaoPackage() && !(getter() || setter()) && excludeMyDemoLoggingAspect() ")
    public void forDaoPackageNoGetterSetter() {}

}

Solution

  • If you use Spring AOP you don't need to be afraid that one aspect will intercept methods from another because Spring does not support that, as is documented in chapter 5.4.2. Declaring an Aspect. Scroll down a bit and look for this info box:

    Advising aspects with other aspects?

    In Spring AOP, aspects themselves cannot be the targets of advice from other aspects. The @Aspect annotation on a class marks it as an aspect and, hence, excludes it from auto-proxying.

    So basically your issue is a non-issue.

    If however you use AspectJ via LTW you are subject to no such limitations and thus have to be careful to exclude other aspects which would normally be intercepted due to matching pointcuts. I recommend to put aspects in packages which are easy to exclude, otherwise you have to do it class name by class name. Use pointcuts like these, depending on your situation:

    !within(com.aop.dao.MyDemoLoggingAspect)
    !within(com.acme.aop..*)
    !within(com.acme..*Aspect)