[英]Spring AOP before and after method not working with XML configuration
我有一個方面,目前沒有什么特別之處:
@Named
public final class PreventNullReturn {
public void replaceNullWithSpecialCase() {
System.out.print("ASPECT CALLED!");
throw new AssertionError();
}
}
它只是應該拋出一個錯誤來證明它被調用。 我有spring-aspects.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: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"
>
<aop:aspectj-autoproxy/>
<bean class="aspects.PreventNullReturn"
id="preventNullReturn"
></bean>
<aop:config>
<aop:aspect
ref="preventNullReturn"
>
<aop:pointcut
id="preventNull"
expression="execution(* *.getById(..))"
></aop:pointcut>
<aop:before
pointcut-ref="preventNull"
method="replaceNullWithSpecialCase"
></aop:before>
<aop:after
pointcut-ref="preventNull"
method="replaceNullWithSpecialCase"
></aop:after>
</aop:aspect>
</aop:config>
</beans>
在Spring單元測試的配置文件中,我以這種方式使用它:
<import resource="classpath*:spring-aspects.xml" />
但是不叫方面。 有趣的是,即使IDE也會顯示“ Navigate to advised methods
工具提示,並且導航正確。 我嘗試遵循第1.1.3. Applying aspects
章中的“ Spring in Action,第四版”一書 , 1.1.3. Applying aspects
(這就是為什么我使用XML而不是類)的原因,但是我做錯了事,無法弄清楚。請寫我可以提供更多有用的信息,如果您決定幫助我,請多謝。
我自己找到了一個答案,而不是將其作為資源導入到Spring單元測試的配置文件中:
<import resource="classpath*:spring-aspects.xml" />
我必須直接將其內容存儲在Spring單元測試的配置文件中:
<!--TODO: Move it to separated file-->
<aop:aspectj-autoproxy/>
<bean class="aspects.PreventNullReturn"
id="preventNullReturn"
></bean>
<aop:config>
<aop:aspect
ref="preventNullReturn"
>
<aop:pointcut
id="preventNull"
expression="execution(* *.getById(..))"
></aop:pointcut>
<aop:before
pointcut-ref="preventNull"
method="replaceNullWithSpecialCase"
></aop:before>
<aop:after
pointcut-ref="preventNull"
method="replaceNullWithSpecialCase"
></aop:after>
</aop:aspect>
</aop:config>
當然,哪一個解決方案比將其存儲在單獨的文件中更糟糕,但這是我可以使其工作的唯一方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.