1、建表
CREATE TABLE `business_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(100) NULL COMMENT '业务名称',
`description` varchar(255) NULL COMMENT '业务操作描述',
`operator` varchar(100) NULL COMMENT '操作人',
`oper_time` datetime NULL COMMENT '操作时间',
`param` varchar(255) NULL COMMENT '操作参数报文',
`ip_from` varchar(50) NULL COMMENT '操作来源ip',
PRIMARY KEY (`id`)
);
1、添加pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、定义业务日志注解,可以作用在控制器或其他业务类上,用于描述当前类的功能,也可以作用在方法上,用于描述当前方法的作用。
/**
* 业务日志注解
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ServiceLog {
/**
* 名称
*/
String name() default "";
/**
* 描述
*/
String description() default "";
}
3、编写切面类,并使用@ServiceLog定义切入点,在环绕通知内执行过目标方法后,获取目标类、目标方法上的业务日志注解上的功能名称、功能描述和参数名称, 作用在方法上的名称可覆盖作用在类上的名称
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.entity.BusinessLog;
import com.example.demo.mapper.BusinessLogMapper;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
@Slf4j
@Component
@Aspect
public class ServiceLogAop implements Ordered {
@Resource
private BusinessLogMapper businessLogMapper;
/**
* 定义ServiceLogAop的切入点为标记@ServiceLog注解的方法
*/
@Pointcut(value = "@annotation(com.example.demo.automation.ServiceLog)")
public void pointcut() {}
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) {
log.info("ServiceLogAop around start");
Object result = null;
//执行目标方法
try {
result = pjp.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
//目标方法执行完成后,获取目标类、目标方法上的业务日志注解上的功能名称和功能描述
Object target = pjp.getTarget();
Object[] args = pjp.getArgs();
MethodSignature signature = (MethodSignature) pjp.getSignature();
ServiceLog log1 = target.getClass().getAnnotation(ServiceLog.class);
ServiceLog log2 = signature.getMethod().getAnnotation(ServiceLog.class);
BusinessLog businessLog = new BusinessLog();
if (log2 != null){
String name = log2.name();
if (StrUtil.hasBlank(name) && log1 != null){
name = log1.name();
}
businessLog.setName(name);
String description = log2.description();
businessLog.setDescription(description);
}
businessLog.setOperator("user");
businessLog.setOperTime(LocalDateTime.now());
businessLog.setParam(JSONUtil.toJsonStr(args));
businessLogMapper.insert(businessLog);
log.info("ServiceLogAop around end");
return result;
}
@Override
public int getOrder() {
return 1;
}
}
4、Controller 测试
@ServiceLog(description = "联表查询")
@GetMapping("/index")
public SearchResult<Map<String, Object>> index(){
return mapSearcher.search(StudentVo.class);
}
@GetMapping("/aop")
@ServiceLog(description = "aop日志测试")
public String aopTest(String username) {
return "test";
}
@GetMapping("/aop2")
@ServiceLog(name = "aop测试2", description = "aop日志测试2")
public String aopTest2() {
return "test2";
}
标签:description,SpringBoot,demo,org,ServiceLog,Aop,import,public,name
From: https://blog.csdn.net/qq_36813853/article/details/137039584