springcloud中利⽤AOP原理记录⽤户的操作⽇志该功能写在⼦模块Common中,其它模块引⽤该模块后在某些⽅法上实现对应的注解即可实现⽤户操作⽇志的记录
1.创建⾃定义注解类OperationAnnotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Holley
* @description 操作⽇志
* @create 2020-08-24 10:37
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationAnnotation {
/**
* 操作内容
* @return
*/
String content();
}
2.实现切⾯通知类OperationAspect
package com.sgeye.cloud.aop;
import com.sgeye.cloud.annotation.OperationAnnotation;
import com.stant.UserConstant;
import com.sgeye.cloud.mapper.OperationMapper;
import com.del.JWToken;
import com.del.Operation;
import com.sgeye.cloud.util.IpUtil;
import com.sgeye.cloud.util.RedisUtils;
slf4j.Slf4j;
import org.apachemons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.flect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import annotation.Order;
import org.springframework.stereotype.Component;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import flect.Method;
import java.util.Arrays;
import java.util.HashMap;
/
**
* @author Holley
* @description 请输⼊⼀句话进⾏描述
* @create 2020-08-24 10:58
**/
@Slf4j
@Order
@Aspect
@Component
public class OperationAspect {
@Autowired
private RedisUtils redisUtils;
@Autowired
private OperationMapper operationMapper;
@After("@annotation(com.sgeye.cloud.annotation.OperationAnnotation)")
public void OperationHandler(JoinPoint joinPoint){
log.info("开始记录操作⽇志》》》》》》》》》》》》》");
ServletRequestAttributes attributes = (RequestAttributes();
HttpServletRequest request = Request();
Operation operation = new Operation();
operation.Ip(request));
// 获取注解
MethodSignature methodSignature = (Signature();
Method method = Method();
operation.Name());
operation.DeclaringClass().getName());
operation.Args()));
OperationAnnotation operationAnnotation = Annotation(OperationAnnotation.class);
operation.t());
String token = Header("token");
if (StringUtils.isNotEmpty(token)) {
JWToken jwToken = new JWToken();
//获取解析token中的值
HashMap<String, Object> map = jwToken.parseJWT(token);
Long uid = null;
String account = null;
Long hid = null;
if (map != null) {
if (("uid") != null && ("uid").toString() != "") {
uid = Long.("uid").toString());
(UserConstant.HOSPITAL + uid) != null){
hid = Long.valueOf(((UserConstant.HOSPITAL + uid));
}
}
if (("account") != null) {
account = (String) ("account");
}
operation.setAccount(account);
operation.setHid(hid);
operation.setUid(uid);
}
}
    // 插⼊数据库
operationMapper.insertOperation(operation);
log.info("结束记录操作⽇志》》》》》》》》》》》》》");
}
spring aop应用场景
}
3.在接⼝上添加注解OperationAnnotation
@GetMapping("/{hid}")
@OperationAnnotation(content = "根据医院hid查询医院详细信息")
public Object getHospitalInfo(@PathVariable("hid") Long hid,
@RequestParam(name = "request_from",required = false)String requestFrom){        HospitalDto hospitalDto = HospitalInfoByHid(hid);
if (CommonConstant.RESQUEST_FROM_APPLET.equals(requestFrom)) {
HospitalVo hospitalVo = DozerUtil.map(hospitalDto,HospitalVo.class);
return hospitalVo;
}
HospitalOfEfileVo hospitalOfEfileVo = DozerUtil.map(hospitalDto,HospitalOfEfileVo.class);
return new Response(hospitalOfEfileVo,"查询成功");
}
参考⽂档:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。