uu dd
uu dd

Reputation: 541

Spring aop: can't find referenced pointcut annotation

I am trying to monitor the running of certain methods. I get this error when I run the program.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arithmeticCalculator' defined in URL [jar:file:/home/android/java/spring-5-recipes-master/spring-recipes-UPDATED-master/ch02/recipe_2_17_i/build/libs/recipe_2_17_i-4.0.0-SNAPSHOT.jar!/com/apress/springrecipes/calculator/ArithmeticCalculatorImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut annotation
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
        at org.springframework.context.support.GenericXmlApplicationContext.<init>(GenericXmlApplicationContext.java:71)
        at com.apress.springrecipes.calculator.Main.main(Main.java:11)
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut annotation
        at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:319)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:226)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.obtainPointcutExpression(AspectJExpressionPointcut.java:197)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:176)
        at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:224)
        at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:283)
        at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:315)
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:124)
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:93)
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:74)
        at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:352)
        at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:304)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:438)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1714)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
        ... 10 more

I define an annotation to mark the methods to be logged. The annotation is

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggingRequired {
}

Then I define a pointcut as

@Aspect
public class CalculatorPointcuts {

    @Pointcut("annotation(com.apress.springrecipes.calculator.LoggingRequired)")
    public void loggingOperation() {}

}

Then I apply the pointcut to an aspect. The aspect is to log the running state of methods.

@Aspect
@Component
public class CalculatorLoggingAspect {

    private Log log = LogFactory.getLog(this.getClass());

    @Before("CalculatorPointcuts.loggingOperation()")
    public void logBefore(JoinPoint joinPoint) {
        log.info("The method " + joinPoint.getSignature().getName()
              + "() begins with " + Arrays.toString(joinPoint.getArgs()));
    }

    @After("CalculatorPointcuts.loggingOperation()")
    public void logAfter(JoinPoint joinPoint) {
        log.info("The method " + joinPoint.getSignature().getName()
                + "() ends");
    }

}

The AritheticCalculator interface and its implementation is

public interface ArithmeticCalculator {

    public double add(double a, double b);
    public double sub(double a, double b);

}

@Component("arithmeticCalculator")
@LoggingRequired
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    public double add(double a, double b) {
        return a + b;
    }

    public double sub(double a, double b) {
        return a - b;
    }

}

Does anyone know where is the problem?

Thanks

Upvotes: 0

Views: 1449

Answers (1)

Barath
Barath

Reputation: 5283

I noticed two things

1) @annotation works at method level not at the class level. change it to @within

@Pointcut("@within(com.apress.springrecipes.calculator.LoggingRequired)")
public void loggingOperation() {}

2) Defining Pointcut in another class referencing will be another issue. Instead define it in the same class


@Aspect
@Component
public class CalculatorLoggingAspect {

    private Log log = LogFactory.getLog(this.getClass());

    @Pointcut("@within(com.apress.springrecipes.calculator.LoggingRequired)")
public void loggingOperation() {}  

    @Before("loggingOperation()")
    public void logBefore(JoinPoint joinPoint) {
        log.info("The method " + joinPoint.getSignature().getName()
              + "() begins with " + Arrays.toString(joinPoint.getArgs()));
    }

    @After("loggingOperation()")
    public void logAfter(JoinPoint joinPoint) {
        log.info("The method " + joinPoint.getSignature().getName()
                + "() ends");
    }

}

GitHub reference issue-57720747-aspect-logging-annotation

Upvotes: 1

Related Questions