步骤一:定义一个自定义注解
import com.acca.opra.arm.enums.IntoLogEnum;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IntoLog {
IntoLogEnum value();
}
public enum IntoLogEnum {
XXXX_INFO("XXX信息","XXX接口名","优先级(排序)");
private String oprName;
private String oprDesc;
private String sort;
}
步骤二:编写切面
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
@Slf4j
@Aspect
@Component
public class LogAspect {
@Autowired
private ArmActJobLogsService jobLogsService;
@Pointcut("@annotation(com.xxx.IntoLog)")
public void LogAspectPoint() {
}
@Around("LogAspectPoint()")
public Object afterReturn(ProceedingJoinPoint joinPoint) throws Throwable {
String oprName = "",oprDesc = "",sort = "";
Class<?> aClass = joinPoint.getTarget().getClass();
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method m : declaredMethods){
if(m.isAnnotationPresent(IntoLog.class)){
oprName = m.getAnnotation(IntoLog.class).value().getOprName();
oprDesc = m.getAnnotation(IntoLog.class).value().getOprDesc();
sort = m.getAnnotation(IntoLog.class).value().getSort();
}
}
try {
log.info("【环绕返回通知】【"+name+"方法返回,返回值:"+proceed+"】");
} catch (Exception e) {
log.info("【环绕异常通知】【"+name+"方法异常,异常信息:"+e+"】");
}
return args;
}
}
步骤三:使用
@IntoLog(IntoLogEnum.XXXX_INFO)
public void run() {
return;
}