是否可以在springframework中记录方法所需的时间[selective |全部]自动我自动意思是,我不想去每个方法并编写log.debug(“....”);东西。
答案 0 :(得分:17)
AOP就是你需要的。 AOP允许您在不修改原始代码的情况下向应用程序添加代码。 Spring AOP更喜欢使用 Proxy 对象来完成此任务。 代理对象使用装饰器模式来包装原始目标对象并添加代码。 代理配置为实现原始目标对象的一个或多个接口。
这里,为了给应用程序计时,我们的想法是使用PerformanceMonitorInterceptor
,它是Spring Framework附带的性能监视类之一。
第一个选项是使用Spring类ProxyFactoryBean
创建Spring AOP 代理对象。要做到这一点:
PerformanceMonitorInterceptor
:RegexpMethodPointcutAdvisor
:ProxyFactoryBean
代理原始bean并应用 Advisor PerformanceMonitorInterceptor
的日志级别设置为 TRACE 在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>
PerformanceMonitorInterceptor
的日志级别配置:
log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE
从Spring 2.0开始,还有另一种选择:使用Spring 2.0 XML Schema-based configuration和Spring的AspectJ style pointcut expressions。使用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>
答案 1 :(得分:4)
您可以查看stagemonitor。它是一个开源的Java Web应用程序性能监视器。它捕获响应时间指标,JVM指标,请求详细信息(包括请求探查器捕获的调用堆栈)等。开销非常低。
或者,您可以使用伟大的时间序列数据库石墨来存储数据点的长历史,您可以使用花哨的仪表板查看这些数据点。
屏幕截图示例:
查看project website以查看更多屏幕截图,功能说明和文档。
注意:我是stagemonitor的开发者
答案 2 :(得分:2)
最后我想出了如何做到这一点。
首先看到' Pascal Thivent '的帖子,它对我有很大的帮助。更改log4j.properties并创建 timingAdvisor 之后,将此顾问绑定到您要启用调试的类。你必须像这样更改你的代码。
早期代码:
<bean id="myTableDao" class="com.xyz.sc.db.dao.MyTableDaoImpl" parent="commonDataSource" >
<property name="anotherDao" ref="anotherDao"/>
</bean>
新代码。
<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>
答案 3 :(得分:1)
您可以使用AspectJ来声明由通配符调用的日志切入点,并使用before()和after()通知进行预处理和后处理。
答案 4 :(得分:1)
我看到这里已经有了一个公认的答案,但我鼓励大家看一下最新版本的Spring Toolsuite(SpringSource的Eclipse发行版)。它带有一个开箱即用的分析工具Spring Insight,可以在运行时以漂亮的格式提供这些精确的统计信息。只需将您的应用程序部署到其内部tomcat,点击几页,然后转到/ insight servlet,查看每个方法所花费的时间,一直到执行的SQL语句以及它们花了多长时间。
这是一个关于Spring Insight的精彩文章的链接,可以在几分钟内获得您想要的内容。 http://www.dotkam.com/2009/10/28/spring-insight-in-action-5-minutes-from-scratch/