java 如何记录 Springframework 中方法所花费的时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1496205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log the time taken by methods in Springframework?
提问by Rakesh Juyal
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.
是否可以在 springframework 中记录方法 [ 选择性 | ] 所花费的时间?所有] 自动。自动我的意思是,我不想去每个方法并编写 log.debug ( "...." ); 东西。
回答by Pascal Thivent
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 Proxyobjects. Proxyobjects use a Decorator Pattern to wrap the original Targetobject and add code. The Proxyis configured to implement one or more interfaces of the original Targetobject.
AOP 正是您所需要的。AOP 允许您在不修改原始代码的情况下向应用程序添加代码。Spring AOP 更喜欢使用Proxy对象来实现这一点。代理对象使用装饰器模式来包装原始目标对象并添加代码。所述代理被配置为实现原始的一个或多个接口的目标对象。
Here, to time an application, the idea is to use the PerformanceMonitorInterceptor, one of the performance monitoring classes that ship with the Spring Framework.
在这里,要对应用程序计时,想法是使用PerformanceMonitorInterceptorSpring 框架附带的性能监视类之一。
The first option is to use the Spring class ProxyFactoryBeanto create Spring AOP Proxyobjects. To do this:
第一个选项是使用 Spring 类ProxyFactoryBean来创建 Spring AOP代理对象。去做这个:
- Define your original bean:
- Define a
PerformanceMonitorInterceptor: - Define a
RegexpMethodPointcutAdvisor: - Define a
ProxyFactoryBeanto proxy your original bean and apply your Advisor - Set the Log level for the
PerformanceMonitorInterceptorto TRACE
- 定义您的原始 bean:
- 定义一个
PerformanceMonitorInterceptor: - 定义一个
RegexpMethodPointcutAdvisor: - 定义一个
ProxyFactoryBean代理您的原始 bean 并应用您的顾问 - 将日志级别设置
PerformanceMonitorInterceptor为TRACE
Below a Spring configuration that illustrates these steps:
下面是说明这些步骤的 Spring 配置:
<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>
And the configuration of the Log level for the PerformanceMonitorInterceptor:
以及日志级别的配置PerformanceMonitorInterceptor:
log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE
Starting with Spring 2.0, there is another option: using Spring 2.0 XML Schema-based configurationand Spring's AspectJ style pointcut expressions. With the ProxyFactoryBeanyou 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.
从 Spring 2.0 开始,还有另一种选择:使用Spring 2.0 基于 XML Schema 的配置和 Spring 的AspectJ 样式切入点表达式。随着ProxyFactoryBean你必须明确地宣布要代理的接口; 使用<aop:config>和<aop:advisor>标签,您可以自动代理 bean 容器中每个对象的每个接口。
<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>
回答by Felix
You can take a look at stagemonitor. It is a open source java web application performance monitor. It captures response time metrics, JVM metrics, request details (including a call stack captured by the request profiler) and more. The overhead is very low.
你可以看看stagemonitor。它是一个开源的 Java Web 应用程序性能监视器。它捕获响应时间指标、JVM 指标、请求详细信息(包括请求分析器捕获的调用堆栈)等。开销非常低。
Optionally, you can use the great timeseries database graphite with it to store a long history of datapoints that you can look at with fancy dashboards.
或者,您可以使用伟大的时间序列数据库石墨来存储数据点的悠久历史,您可以使用精美的仪表板查看这些数据点。
Example Screenshot:

示例截图:

Take a look at the project websiteto see more screenshots, feature descriptions and documentation.
查看项目网站以查看更多屏幕截图、功能描述和文档。
Note: I am the developer of stagemonitor
注:我是stagemonitor的开发者
回答by Rakesh Juyal
Finally i figured out how to do this.
最后我想出了如何做到这一点。
First of all see the post by 'Pascal Thivent', it did a great help to me. After changing your log4j.properties and creating the timingAdvisorwhat you have to is, binding this adviser to the class you wan to enable the debugging. You have to change your code like this.
首先看' Pascal Thivent'的帖子,它对我帮助很大。在更改您的 log4j.properties 并创建timingAdvisor 之后,您必须将这个顾问绑定到您想要启用调试的类。您必须像这样更改代码。
earlier code:
之前的代码:
<bean id="myTableDao" class="com.xyz.sc.db.dao.MyTableDaoImpl" parent="commonDataSource" >
<property name="anotherDao" ref="anotherDao"/>
</bean>
New code.
新代码。
<bean id="myTableDaoTarget" class="com.xyz.sc.db.dao.MyTableDaoImpl" parent="commonDataSource" >
<property name="anotherDao" ref="anotherDao"/>
</bean>
<bean id="myTableDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.xyz.sc.db.dao.MyTableDao</value>
</property>
<property name="target"><ref local="myTableDaoTarget"/></property>
<property name="interceptorNames">
<list>
<value>timingAdvisor</value>
</list>
</property>
</bean>
回答by Rakesh Juyal
I see that there has already been an accepted answer here, but I'd encourage everyone to take a look at the latest release of Spring Toolsuite (SpringSource's distro of Eclipse). It comes with a profiling utility out of the box, Spring Insight, that provides these exact statistics at runtime in a nice format. Just deploy your app to its internal tomcat, hit a few pages, then go to the /insight servlet and see the time taken in each method called all the way down to the SQL statements that were executed and how long they took.
我看到这里已经有一个可以接受的答案,但我鼓励大家看看 Spring Toolssuite 的最新版本(SpringSource 的 Eclipse 发行版)。它带有一个开箱即用的分析实用程序 Spring Insight,它在运行时以一种很好的格式提供这些准确的统计信息。只需将您的应用程序部署到其内部 tomcat,点击几页,然后转到 /insight servlet,查看调用的每个方法所花费的时间,一直到执行的 SQL 语句以及它们花费的时间。
Here's a link to a nice writeup about Spring Insight that should get you what you want in just a few minutes. http://www.dotkam.com/2009/10/28/spring-insight-in-action-5-minutes-from-scratch/
这是一篇关于 Spring Insight 的精彩文章的链接,它应该会在几分钟内为您提供所需的内容。http://www.dotkam.com/2009/10/28/spring-insight-in-action-5-minutes-from-scratch/

