in Java, Programming

Spring: error at ::0 can’t find referenced pointcut的错误并解决

问题

运行Spring测试程序,返回错误: AOP : java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut.

程序如下:

AspectJBean.java

package springcore.aspectj;

public class AspectJBean {

    public void display() {
        System.out.println( "AOPBean.display()!" );
    }
}

AspectJMain.java

package springcore.aspectj;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectJMain {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext( "springcore/aspectj/AspectJBeans.xml" );

        AspectJBean aspectJBean = (AspectJBean)factory.getBean( "aspectJBean" );
        aspectJBean.display();

    }
}

LoggerAspect.java

package springcore.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggerAspect {

    @Pointcut("execution(public * *(..))")
    public void publicMethods() { }

    @Pointcut("execution(* springcore.aspectj.LoggerAspect.*(..))")
    public void logObjectCalls() { }

    @Pointcut("publicMethods()&&!logObjectCalls()")
    public void loggableCalls() { }


    @Around("loggableCalls()")
    public Object aroundLogCalls(ProceedingJoinPoint joinPoint)
            throws Throwable {
        System.out.println("before invoke method:"
                + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("after invoke method:"
                + joinPoint.getSignature().getName());
        return object;
    }

}

AspectJBeans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <aop:aspectj-autoproxy/>

    <bean id="loggerAspect" class="springcore.aspectj.LoggerAspect"/>
    <bean id="aspectJBean" class="springcore.aspectj.AspectJBean"/>
</beans>

解决

参考stackoverflow: AOP : java.lang.IllegalArgumentException: error at ::0 can’t find referenced pointcut

需要使用更高级一点的版本的aspectjrt.jaraspectj-weaver.jar, 进行Google,然后下载后进行替换,问题解决。

以上问题解决。显示如下:

before invoke method:display
AOPBean.display()!
after invoke method:display

参考

stackoverflow: AOP : java.lang.IllegalArgumentException: error at ::0 can’t find referenced pointcut