【问题标题】:spring AOP error at 0 can't find referenced pointcut0处的spring AOP错误找不到引用的切入点
【发布时间】:2019-08-23 14:56:49
【问题描述】:

我正在使用 Java 8、spring 4.3 和 AspectJ 1.8.9。为什么我收到以下代码的以下错误。如果我在没有切入点的情况下使用 @Before("com.beans.Student.addCustomer()") 我会在 0 处收到此错误,找不到引用的切入点。将@Before 与切入点一起使用时,我没有收到错误消息。

豆子:

@Aspect
public class Beforeaspect {

    /*
     * @Pointcut("execution(* com.beans.Student.addCustomer(..))") public void
     * log(){
     * }
     */

    // @Before("log()")
    @Before("com.beans.Student.addCustomer()")
    public void logBefore(JoinPoint jp) {
        System.out.println("logbefore");
        System.out.println("method " + jp.getSignature().getName());
    }
}

学生:

package com.beans;

public class Student implements Studentimpl {

    public void addCustomer() {
        System.out.println("addcustomer");
    }

    public String addCustomername(String stud) {
        System.out.println("addcustomername");
        return "hello";
    }
}

Spring xml 文件:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <aop:aspectj-autoproxy />
    <bean id="stud" class="com.beans.Student" />
    <bean class="com.beans.Beforeaspect" />
</beans>

【问题讨论】:

    标签: java spring spring-boot aop spring-aop


    【解决方案1】:

    您的方法执行使用了错误的语法。您的注释应该是:

    @Before("execution(* com.beans.Student.addCustomer(..))")
    public void logBefore(JoinPoint jp) {
        System.out.println("logbefore");
        System.out.println("method " + jp.getSignature().getName());
    }
    

    或者使用 XML Bean:

    <aop:aspectj-autoproxy />
    
    <bean id="logAspect" class="nch.spring.aop.aspectj.LoggingAspect" />
    
    <aop:config>
        <aop:aspect id="aspectLoggging" ref="logAspect">
            <aop:pointcut id="pointCutBefore" expression="execution(* com.beans.Student.addCustomer(..)))" />
            <aop:before method="logBefore" pointcut-ref="pointCutBefore" />
        <aop:aspect/>
    <aop:config>
    

    【讨论】:

    • 谢谢 Nikolas,我忘了添加下面的 public class Student{ @Pointcut("execution(* com.beans.Student.addCustomers(..))") void addCustomer(){ } }跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2010-10-02
    相关资源
    最近更新 更多