如何记录Springframework中方法所花费的时间? [英] How to log the time taken by methods in Springframework?

查看:100
本文介绍了如何记录Springframework中方法所花费的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在springframework中记录方法所花费的时间[selective |全部]自动我自动意思是,我不想去每个方法并写入log.debug(....);东西。

Is it possible in springframework to log the time taken by methods [ selective | all ] automatically. By automatically i mean, i don't want to go to each method and write the log.debug ( "...." ); stuff.

推荐答案

AOP就是你需要的。 AOP允许您在不修改原始代码的情况下向应用程序添加代码。 Spring AOP更喜欢使用 Proxy 对象来完成此任务。 代理对象使用装饰器模式来包装原始目标对象并添加代码。 代理配置为实现原始目标对象的一个​​或多个接口。

AOP is what you need here. AOP allows you to add code to your application without modifying the original code. Spring AOP prefers to accomplish this with Proxy objects. Proxy objects use a Decorator Pattern to wrap the original Target object and add code. The Proxy is configured to implement one or more interfaces of the original Target object.

这里,为了给应用程序计时,我们的想法是使用 PerformanceMonitorInterceptor ,随附的性能监控类之一Spring框架。

Here, to time an application, the idea is to use the PerformanceMonitorInterceptor, one of the performance monitoring classes that ship with the Spring Framework.

第一个选项是使用Spring类 ProxyFactoryBean 来创建Spring AOP 代理对象。为此:

The first option is to use the Spring class ProxyFactoryBean to create Spring AOP Proxy objects. To do this:


  • 定义原始bean

  • 定义a PerformanceMonitorInterceptor

  • 定义 RegexpMethodPointcutAdvisor

  • 定义 ProxyFactoryBean 代理原始bean并应用 Advisor

  • 设置 PerformanceMonitorInterceptor 的日志级别为 TRACE

  • Define your original bean:
  • Define a PerformanceMonitorInterceptor:
  • Define a RegexpMethodPointcutAdvisor:
  • Define a ProxyFactoryBean to proxy your original bean and apply your Advisor
  • Set the Log level for the PerformanceMonitorInterceptor to TRACE

在Spring配置下面说明了这些步骤:

Below a Spring configuration that illustrates these steps:

<beans>
  <bean id="MyServiceTarget" class="org.myapp.services.MyService">
    <property ... />
  </bean>

  <bean id="timingLogger" class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"/>

  <bean id="timingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="timingLogger"/>
    <property name="patterns">
      <list>
        <value>.*</value>
      </list>
    </property>
  </bean>

  <bean id="MyService" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>org.myapp.services.MyService</value>
    </property>
    <property name="target"><ref local="MyServiceTarget"/></property>
    <property name="interceptorNames">
      <list>
        <value>timingAdvisor</value>
      </list>
    </property>
  </bean>
</beans>

PerformanceMonitorInterceptor

log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE

从Spring 2.0开始,还有另一种选择:使用基于Spring 2.0 XML Schema的配置和Spring的 AspectJ样式切入点表达式。使用 ProxyFactoryBean ,您必须显式声明要代理的接口;使用< aop:config> < aop:advisor> 标签,您可以自动代理每个界面bean容器中的每个对象。

Starting with Spring 2.0, there is another option: using Spring 2.0 XML Schema-based configuration and Spring's AspectJ style pointcut expressions. With the ProxyFactoryBean you have to explicitly declare the interfaces you want to proxy; using the <aop:config> and <aop:advisor> tags, you can automatically proxy every interface of every object in the bean container.

<beans "add xsd declarations here" >
  <bean id="MyService" class="org.myapp.services.MyService">
    <property ... />
  </bean>

  <bean id="timingAdvice"
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"/>

  <aop:config>
    <aop:advisor pointcut="execution(* org.myapp.services.MyService.*(..))"
      advice-ref="timingAdvice"/>
  </aop:config>
</beans>

这篇关于如何记录Springframework中方法所花费的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆