spring aop日志简单处理

  1. pom依赖:
<dependency>
    <groupId>org.aspectj</groupId>
     <artifactId>aspectjrt</artifactId>
     <version>1.6.12</version>
</dependency>
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.6.12</version>
</dependency>
  1. 自定义接口注解类:
import java.lang.annotation.*;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

@Target({METHOD,TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Operation {
    String name();
}
  1. 切面操作类:
import com.zbk.entity.LogBook;
import com.zbk.entity.User;
import com.zbk.tools.Operation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class BookAOP {

    @Autowired
    private LogBookService logBookService;

    @Pointcut("execution(* com.zbk.controller..*.*(..))")
    public void method(){}
    
    @Pointcut("execution(* com.zbk.controller..*.*(..))") //切点
    public void webExceptionLog(){}


    /**
     * 正常日志信息
     * @param joinPoint
     */
    @AfterReturning("method()")
    public void afterReturning(JoinPoint joinPoint){
        insertLogBook(joinPoint,"正常");
    }

    /** 
      * 异常通知 用于拦截异常日志 
      * @param joinPoint 
      * @param e 
      */
    @AfterThrowing(pointcut = "webExceptionLog()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
        insertLogBook(joinPoint,"错误");
    }

    private void insertLogBook(JoinPoint joinPoint,String logType){
        LogBook logBook = new LogBook();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 2:通过springAOP切面JoinPoint类对象,获取该类,或者该方法,或者该方法的参数
        Class<? extends Object> clazz =  joinPoint.getTarget().getClass();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Method method = signature.getMethod();
        Method[] methods = clazz.getDeclaredMethods();
        String methodOperation = "";
        for (Method m : methods) {
            if (m.equals(method)) {
                methodOperation = m.getName();
                if (m.isAnnotationPresent(Operation.class)) {
                    methodOperation = m.getAnnotation(Operation.class).name();
                }
            }
        }
        Date date = new Date();
        logBook.setCreatTime(date);
        logBook.setLogContent(methodOperation);
        logBook.setUserOperation(user.getUserName());
        logBook.setLogType(logType);
        logBookService.insertLogBook(logBook);
    }
}
  1. 使用
@Operation(name="查询字典列表")
@ResponseBody
@RequestMapping(value = "/getDataDictionaryList")
public Map<String,Object> getDataDictionaryList(@RequestParam Map<String, Object> params){
   int totalCount = dataDictionaryService.dataCount(params);
   Map<String, Object> rePutParams = LayuiUtil.rePutParams(params);
   List<DataDictionary> dataDictionaryList = dataDictionaryService.getDataDictionaryList(rePutParams);
   return LayuiUtil.dataList(totalCount, dataDictionaryList);
}
  1. 效果
    在这里插入图片描述
    有问题望指正!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值