import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
private volatile long time=0L;
//controller包的子包里面任何方法
@Pointcut("execution(public * com.cource.controller.*.*(..))")
public void log(){
}
@Before("log()")
public void before(){
time=System.currentTimeMillis();
System.out.println("before==================================>"+time);
}
@AfterReturning("log()")
public void after(){
time=System.currentTimeMillis()-time;
System.out.println("after=====================================>"+time);
}
//抛出异常时才调用
@AfterThrowing("log()")
public void afterThrowing()
{
System.out.println("afterThrowing......");
}
}
增加以上类,路径放在spring的扫描包下
在spring的配置文件中增加
<aop:aspectj-autoproxy proxy-target-class="true" />
spring默认是以接口代理的,该注解的意思是让spring以类代理,因为controller层没有实现接口。