Spring AOP 前后方法不适用于 XML 配置
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" />
但是没有调用 aspect。 有趣的是,甚至 IDE 显示 Navigate to advised methods
工具提示并且导航正确。我尝试遵循 1.1.3. Applying aspects
章中的 "Spring in Action, Fourth Edition book(这就是我使用 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>
这当然比将它放在单独的文件中更糟糕,但这是我让它工作的唯一方法。
我有一个方面,目前没有做任何特别的事情:
@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" />
但是没有调用 aspect。 有趣的是,甚至 IDE 显示 Navigate to advised methods
工具提示并且导航正确。我尝试遵循 1.1.3. Applying aspects
章中的 "Spring in Action, Fourth Edition book(这就是我使用 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>
这当然比将它放在单独的文件中更糟糕,但这是我让它工作的唯一方法。