Tôi đã thực hiện việc này bằng Spring AOP.
Thỉnh thoảng tôi cần thông tin về thời gian thực hiện một số phương pháp trong dự án của tôi (ví dụ như phương thức của Bộ điều khiển).
Trong servlet xml tôi đặt
<aop:aspectj-autoproxy/>
Ngoài ra, tôi cần phải tạo ra các lớp cho các khía cạnh:
@Component
@Aspect
public class SystemArchitecture {
@Pointcut("execution(* org.mywebapp.controller..*.*(..))")
public void businessController() {
}
}
Và profiler khía cạnh:
@Component
@Aspect
public 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()));
}
}
Đó là tất cả. Khi tôi yêu cầu một trang từ webapp của tôi, thông tin về thời gian thực hiện phương pháp và việc sử dụng bộ nhớ JVM sẽ được in ra trong tệp nhật ký webapp của tôi.
Nguồn
2010-03-19 08:31:41
Việc lập hồ sơ ứng dụng Spring không thực sự khác biệt trong việc định cấu hình bất kỳ ứng dụng java nào khác. Bạn có thể muốn mở rộng câu hỏi, để có được lựa chọn câu trả lời rộng hơn. – skaffman