【发布时间】:2016-05-29 21:50:22
【问题描述】:
我有一个方面,此时它没有做任何特别的事情:
@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, Fourth Edition book(这就是我使用 XML 而不是类的原因),但我做错了,我无法弄清楚。如果我能提供更多有用的细节,请写信。如果您决定帮助我,我将不胜感激,在此先感谢您。
【问题讨论】:
标签: java spring spring-mvc aop spring-aop