【问题标题】:Spring AOP before and after method not working with XML configurationSpring AOP 之前和之后方法不使用 XML 配置
【发布时间】: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


    【解决方案1】:

    我自己找到了答案,而是将其作为资源导入到 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>
    

    这当然比将它放在单独的文件中更糟糕的解决方案,但这是我让它工作的唯一方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-26
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2018-10-12
      相关资源
      最近更新 更多