gpt4 book ai didi

java - spring AOP错误为0找不到引用的切入点

转载 作者:行者123 更新时间:2023-12-02 03:25:03 29 4
gpt4 key购买 nike

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

bean 类:

@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>

最佳答案

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

@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>

关于java - spring AOP错误为0找不到引用的切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39061033/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com