基于aop的自定义注解统一日志处理功能

基于aop的自定义注解统一日志处理功能

首先增加一个注解接口类

需要定义此注解的作用域(@Target:方法,类,变量,接口)和作用时间(@Retention:资源加载时,资源编译时,运行时)

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BizOperateLog{
	String sysCode() default "";
	String operateId() default "";
	String operateName() default "";
	String operateType() default "";
}

增加aop切入点

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
/*
 * @author:wj
 * @date: 2022/03/20
 * @descrition:自定义日志处理
*/
@Aspect
@Component
public class BizOperateLogAop{
	@Pointcut(value = "@annotation(com.erdp.comm.annotation.BizOperateLog)")
	public void log(){
		//切入点日志开始。。。
	}
	@Before("log()")
	public void beforeExec(JoinPoint joinPoint){
		//输出日志之前进行的操作,可以计算执行时间等。。。
	}

	@Before("log()")
	public void aroundExec(JoinPoint joinPoint){
		//开始自定义处理日志信息。。。
		Object[] orgs = joinPoint.getArgs();
		//这里面能拿到所有注解写入的参数,直接获取然后走自己的业务逻辑即可
		MethodSignature ms = (MethodSignature) jpinPoint.getSignature();
		Method method = ms.getMethod();
		BizOperateLog log = ms.getAnnotation(BizOperateLog.class);
		String sysCode = log.sysCode();
	}
		
	@Before("log()")
	public void aftereExec(JoinPoint joinPoint){
		//输出日志之后进行的操作,可以计算执行时间等。。。
	}
}

业务逻辑使用


package com.jd.controller;

import com.jd.entity.Config;
import com.jd.service.ConfigService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Config)表控制层
 *
 * @author makejava
 * @since 2022-06-15 08:47:03
 */
@RestController
@RequestMapping("config")
public class ConfigController {
    /**
     * 服务对象
     */
    @Resource
    private
    ConfigService configService;
    
    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */

   	@BizOperateLog(sysCode = "AJGL",operateName = "查询",operateType = "操作类型",operateId = "operateId^主键")
    @GetMapping("{id}")
    public ResponseEntity<Config> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.configService.queryById(id));
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值