一、引入jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
二、在applicationContext-framework.xml引入<aop:aspectj-autoproxy/>
注意:xml文件头加上:
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="…… http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
三、定义注解
@Target({ElementType.METHOD ,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperateLogAnnotation {
String value() default "";
}
四、操作日志注解
@Aspect
@Component
public class OperateLogAspect {
@Pointcut("@annotation(com.ckt.util.annotation.OperateLogAnnotation)")
public void operateLogAspect() {
}
/**
* 环绕通知:目标方法执行前后分别执行一些代码,发生异常的时候执行另外一些代码
* @return
*/
@Around(value="operateLogAspect()")
public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
//执行目标方法
Object result = pjp.proceed(pjp.getArgs());
//异步保存操作日志
saveOperationLog(pjp.getSignature().getName() ,pjp.getArgs() ,result);
return result;
}
}