LogOperation 日志注解
/**
* <p>
* <code>LogOperation</code>日志注解
* </p>
*
* @author yhdiao
* @version 1.0
* @date 2020/10/22 14:18
* @since 1.0
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogOperation {
}
LogAdvisor 日志通知器
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.factory.annotation.Autowired;
/**
* <p>
* <code>LogAdvisor</code> 日志通知器
* </p>
*
* @author yhdiao
* @version 1.0
* @since 1.0
*/
public class LogAdvisor extends AbstractPointcutAdvisor {
/**
* 切点
*/
private Pointcut pointcut;
@Autowired
LogAroundAdvice logAroundAdvice;
public LogAdvisor() {
this.pointcut = buildPointcut();
}
/**
* <p>
* <code>getPointcut</code> 切点
* </p>
*
* @return {@link org.springframework.aop.Pointcut}
* @author yhdiao
*/
@Override
public Pointcut getPointcut() {
return this.pointcut;
}
/**
* <p>
* <code>getAdvice</code> 通知
* </p>
*
* @return {@link org.aopalliance.aop.Advice}
* @author yhdiao
*/
@Override
public Advice getAdvice() {
//return new LogAroundAdvice(new LogOperationHandler());
return this.logAroundAdvice;
}
/**
* <p>
* <code>buildPointcut</code> 创建切点
* </p>
*
* @return {@link org.springframework.aop.Pointcut}
* @author yhdiao
*/
private Pointcut buildPointcut() {
Pointcut cpc = new AnnotationMatchingPointcut(LogOperation.class, true);
Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(LogOperation.class);
return new ComposablePointcut(cpc).union(mpc);
}
}
LogAroundAdvice 日志环绕通知
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
/**
* <p>
* <code>LogAroundAdvice</code> 日志环绕通知
* </p>
*
* @author yhdiao
* @version 1.0
* @since 1.0
*/
@Slf4j
public class LogAroundAdvice implements MethodInterceptor {
//@Autowired
//private OperationHandler operationHandler;
/*public LogAroundAdvice(OperationHandler operationHandler) {
this.operationHandler = operationHandler;
}*/
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
long beginTime = System.currentTimeMillis();
Object result = invocation.proceed();
long time = System.currentTimeMillis() - beginTime;
// 日志业务处理
//operationHandler.handle(invocation, time, result);
return result;
}
}
AopConfig 编程式aop配置
/**
* <p>
* <code>AopConfig</code> 编程式aop配置
* </p>
*
* @author yhdiao
* @version 1.0
* @since 1.0
*/
@Configuration
public class AopConfig {
@Bean
public LogAdvisor advisor() {
LogAdvisor advisor = new LogAdvisor();
advisor.setOrder(0);
return advisor;
}
@Bean
public LogAroundAdvice logAroundAdvice() {
return new LogAroundAdvice();
}
}