我是AOP的新手,尤其是Spring AOP。
我想用一些特定的方法来记录执行时间。我阅读了Spring文档,认为最好的解决方案是创建带有注释切入点的方面。
看起来是这样的:
@Around("@annotation(com.x.y.MethodExecutionTime)")
public Object methodExecutionTimeLog(ProceedingJoinPoint joinPoint) throws Throwable
StopWatch stopWatch = new StopWatch();
Object retVal = null;
try {
stopWatch.start();
retVal = joinPoint.proceed();
stopWatch.stop();
logger.info("Execution time: " + stopWatch.getTotalTimeMillis() + " ms");
} catch(Throwable e) {
logger.error("Execution time: " + stopWatch.getTotalTimeMillis() + " ms");
throw e;
}
return retVal;
}在方法中使用注释:
@Override
@MethodExecutionTime
public <T> T copy(Class<T> destType) {
T t = ReflectionHelper.newInstance(destType);
copyTo(t);
return t;
}Spring XML配置:
<context:spring-configured />
<aop:aspectj-autoproxy proxy-target-class="true" />但它什么也没记录。
我使用的是Spring 3.0.5
有什么想法吗?谢谢
发布于 2012-06-19 00:11:35
如果其他一切都配置正确,那么这应该是Spring AOP的限制之一(至少是它的默认配置),即:
应用方面的
new)<aop:aspectj-autoproxy>的另一个方法时,不应用方面应该在与要应用方面的对象相同的应用程序上下文中声明(特别是在典型的Spring Web MVC应用程序中,applicationContext.xml和...-servlet.xml来自不同的应用程序上下文)https://stackoverflow.com/questions/11085125
复制相似问题