import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MethodTimeAdvice implements MethodInterceptor {
private final static Logger logger = LoggerFactory
.getLogger(MethodTimeAdvice.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
StopWatch clock = new StopWatch();
clock.start();
Object result = null;
String className = invocation.getMethod().getDeclaringClass().getSimpleName();
String methodName = className + "." + invocation.getMethod().getName();
result = invocation.proceed();
clock.stop();
logger.info("cost time:" + clock.getTime()/1000 + " s [" + methodName + "]");
System.out.println("cost time:" + clock.getTime()/1000 + " s [" + methodName + "]");
return result;
}
}
配置文件
<bean id="methodTimeAdvice" class="com.---.server.aop.advice.MethodTimeAdvice"/>
<aop:config>
<aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice" pointcut="execution(* com.---.server.jobs.*.*(..))" />
</aop:config>