Spring AOP为非服务类

时间:2012-12-24 15:39:34

标签: java spring cxf aop spring-aop

我是Spring框架的初学者。我已经实现了Spring AOP来记录方法执行时间。使用来自互联网的一些例子,我得到它的服务接口,如下所述。但是,如果我将表达式更改为非服务类,则相同的代码无效。请在CXF配置下面给出。

<bean id="xbean" class="com........xServiceImpl" />

<jaxrs:server id="xServiceRS" address="/xRSService">
    <jaxrs:serviceBeans>
        <ref bean="xbean" />
    </jaxrs:serviceBeans>
</jaxrs:server>

<bean id="performanceLoggingAdvice" class="com......PerformanceLoggingAdvice" />

<aop:config>
    <aop:pointcut id="performanceLoggingPointcut"
        expression="execution(* com.....xService.*(..))" />
    <aop:advisor advice-ref="performanceLoggingAdvice"
        pointcut-ref="performanceLoggingPointcut" id="performanceLoggingInterceptorAdvisor" />
</aop:config>

我已经在堆栈溢出中搜索了类似的问题但我没有得到有用和具体的答案我的问题。知道可能是什么问题吗?如何让它适用于非服务类,例如Utils.java?

先谢谢你帮助我..

1 个答案:

答案 0 :(得分:1)

Spring AOP建议仅适用于声明为Spring bean的类的实例。使你的Util类成为一个Spring bean,使用正确的切入点,它应该可以工作。

在这种情况下,您应该仅通过Spring应用程序上下文获取Util实例(通过调用ApplicationContext.getBean方法之一),而不是通过使用new调用构造函数。