作用AOP监控方法的运行时间如下:
@Component @Aspect public class LogAop { private Logger log = LoggerFactory.getLogger(getClass()); //切面 private final String CUT = "execution (* com.wey.spring.controller.*Controller.*(..))"; @Around(CUT) public Object around(ProceedingJoinPoint pjp) throws Throwable { Object[] args = pjp.getArgs(); Object result = null; long start = System.currentTimeMillis(); try { result = pjp.proceed(args); } catch (Exception e) { throw e; } long end = System.currentTimeMillis(); log.info("" + pjp.getTarget().getClass() + "." + pjp.getSignature().getName() + " 方法用了 " + (end - start) + "毫秒"); return result; } }
注解方式配置开启动态代理:
@EnableAspectJAutoProxy
XML配置方式的
<aop:aspectj-autoproxy proxy-target-class="true"/>