gpt4 book ai didi

java - Spring AOP与AspectJ不拦截

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

我正在编写我的第一个 AOP。我粘贴了下面的代码,该代码在方法调用时不会被拦截。我不确定可能是什么原因。

在控制台上它只打印:

addCustomerAround() 正在运行,参数:虚拟

并且没有打印任何 AOP 建议代码。

有人可以帮忙吗?

界面:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


@Autowired
void addCustomerAround(String name);
}

类别:

package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

public void addCustomerAround(String name){
System.out.println("addCustomerAround() is running, args : " + name);
}
}

AOP:

package com.test.model;

import java.util.Arrays;

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 TestAdvice{


@Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

System.out.println("testAdvice() is running!");
System.out.println("hijacked method : " + joinPoint.getSignature().getName());
System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));

System.out.println("Around before is running!");
joinPoint.proceed();
System.out.println("Around after is running!");

System.out.println("******");

}
}

appcontext.xml:

<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />

POM:

<!-- AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>

方法调用:

aoptest.addCustomerAround("dummy");

最佳答案

我在这里没有看到任何错误。我刚刚尝试过你的代码,效果很好。我的测试类:

public class App {
public static void main(String[] args) throws Exception {

ApplicationContext appContext = new ClassPathXmlApplicationContext("appcontext.xml");

//-------------------------
AopClass aopClass = (AopClass) appContext.getBean("AopClass");
aopClass.addCustomerAround("dummy");
}
}

enter image description here结果

您可以重试构建您的 Maven 项目吗?!然后再试一次?!

关于java - Spring AOP与AspectJ不拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32776017/

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