我已经使用Spring AOP做到了。
有时,我需要有关在项目中执行某些方法(例如,控制器的方法)花费多少时间的信息。
在servlet xml中,我把
<aop:aspectj-autoproxy/>
另外,我需要为方面创建类:
@Component@Aspectpublic class SystemArchitecture { @Pointcut("execution(* org.mywebapp.controller..*.*(..))") public void businessController() { }}和探查器方面:
@Component@Aspectpublic class TimeExecutionProfiler { private static final Logger logger = LoggerFactory.getLogger(TimeExecutionProfiler.class); @Around("org.mywebapp.util.aspects.SystemArchitecture.businessController()") public Object profile(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); logger.info("ServicesProfiler.profile(): Going to call the method: {}", pjp.getSignature().getName()); Object output = pjp.proceed(); logger.info("ServicesProfiler.profile(): Method execution completed."); long elapsedTime = System.currentTimeMillis() - start; logger.info("ServicesProfiler.profile(): Method execution time: " + elapsedTime + " milliseconds."); return output; } @After("org.mywebapp.util.aspects.SystemArchitecture.businessController()") public void profileMemory() { logger.info("JVM memory in use = {}", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); }}就这样。当我从Web应用程序请求页面时,有关方法执行时间和JVM内存使用情况的信息会在我的Web应用程序的日志文件中打印出来。



