[英]spring + aspectj, define an aspect @Around
我想為@Entity的方法定義一個@Around方面
我的所有實體都在包data.entity中
定義這樣的方面:
@Aspect
public class TestAspect {
@Around("execution(* data.entity..*(..))")
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("INTERCEPT: "+pjp.toLongString());
return pjp.proceed();
}
}
但是從來沒有被截獲......我的錯誤在哪里?
在春天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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="data.dao, data.service" />
<tx:annotation-driven proxy-target-class="true"/>
<aop:aspectj-autoproxy/>
<bean id="testAspect" class="spring.TestAspect" />
... datasource and other ...
</beans>
我也試試
@Around("target(data.entity.MyEntity)")
和
@Around("target(data.entity..)")
但仍然不行。
謝謝。
看起來你使用spring-proxy-aop。 僅當類是spring manged bean時才有效,並且必須從其他對象調用adviced方法。
嘗試使用真正的aspectJ而不是spring-proxy-aop。
我剛剛開始使用AOP,以下是我的水平調查結果
我假設您的應用程序類路徑中存在必要的jar文件,aspectjweaver-1.6.10.jar和org.springframework.aop-3.0.5.RELEASE.jar。
您正在定義的aroundAdvice方法是完美的。
你可以刪除下面的行並嘗試。
<context:component-scan base-package =“data.dao,data.service”/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.