[英]Spring AOP with AspectJ not intercepting
我正在寫我的第一個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");
}
}
您可以重試構建您的Maven項目嗎? 然后再試一次?!
根據使用Spring的面向方面的編程 ,當一個類實現一個或多個接口時,將使用JDK動態代理,如果未實現任何接口,則將創建CGLIB代理。 要強制CGLIB代理,您必須設置proxy-target-class =“ true”。
appcontext.xml:
<!-- Aspect -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
輸出:
testAdvice() is running!
hijacked method : addCustomerAround
hijacked arguments : [dummy]
Around before is running!
addCustomerAround() is running, args : dummy
Around after is running!
******
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.