SpringBoot⾃定义注解属性值读取application.properties中的
动态值
⼀、依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
⼆、⾃定义注解
package com.zhyjohn.admin.authc;
import java.lang.annotation.*;
/**
* 系统⽇志注解
*
* @author ZHY
* @version 1.0
*/
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLogAnn {
/**
* ⽇志类型(1登录⽇志,2操作⽇志,3异常⽇志,4 接⼝⽇志)
**/
int type() default 2;
/**
* 操作值
* @return
*/
String operationValue() default "";
/**
* 操作码
* @return
*/
String operationCode() default "";
/**
* 操作码 1 运营端 2 商家端
*/
int  opType() default 1;
}
三、控制器的
@SysLogAnn(operationValue = "设置成功",operationCode ="${zkt.log.operatecode.doLogin}")    public String testDemo(){
System.out.println("设置成功");
redisUtils.set("name", name);
System.out.println("设置成功");
return "设置成功";
}
四、切⾯⽇志
package com.zhyjohn.admin.authc;
import com.zhyjohn.admin.dao.OperateLogDao;
import com.ity.OperateLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.flect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import nv.Environment;
import org.springframework.stereotype.Component;
import flect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
/**
springboot aop* 系统⽇志,切⾯处理类
*
* @author ZHY
* @version 1.0
*/
@Aspect
@Component
public class SysLogAspect{
@Autowired
private Environment environment;
private static final Logger logger = Logger(SysLogAspect.class);
@Autowired
private OperateLogDao operationLogDao;
/**
* 设置操作⽇志切⼊点记录操作⽇志在注解的位置切⼊代码
**/
@Pointcut("@annotation(com.zhyjohn.admin.authc.SysLogAnn)")
public void operateLogPointCut() {
}
/**
* 设置操作异常切⼊点记录异常⽇志扫描所有controller包下操作
*/
@Pointcut("execution(* com.zhyjohn.admin.*.controller..*.*(..))")
public void exceptionLogPointCut() {
}
/**
* 正常返回通知,拦截⽤户操作⽇志,连接点正常执⾏完成后执⾏,如果连接点抛出异常,则不会执⾏    */
@AfterReturning(value = "operateLogPointCut()", returning = "result")
public void saveOperateLog(JoinPoint point, Object result) {
//        ResponseResult responseResult = (ResponseResult) result;
// 获取当前请求⽅法签名
MethodSignature signature = (MethodSignature) Signature();
// 当前登陆⽤户
Method method = Method();
SysLogAnn logAnn = Annotation(SysLogAnn.class);
String operationCode = solvePlaceholders(logAnn.operationCode());
//保存操作
OperateLog log = new OperateLog();
log.setOperateCode(operationCode);
log.setDescription(logAnn.operationValue());
log.w());
//保存记录值
operationLogDao.insertLog(log);
logger.info("=========SysLogAspect添加到系统⽇志==========");
}
/**
* 异常返回通知,⽤于拦截异常⽇志信息连接点抛出异常后执⾏
**/
@AfterThrowing(pointcut = "exceptionLogPointCut()", throwing = "e")
public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
// 获取当前请求⽅法签名
MethodSignature signature = (MethodSignature) Signature();
// 当前登陆⽤户
Method method = Method();
SysLogAnn logAnn = Annotation(SysLogAnn.class);
/
/保存操作
OperateLog log = new OperateLog();
log.solvePlaceholders(logAnn.operationCode()));
log.setDescription(logAnn.operationValue());
log.w());
//保存记录值
operationLogDao.insertLog(log);
logger.info("=========SysLogAspect添加异常⽇志==========");
}
/**
* 转换 request 请求参数
*
* @param paramMap request获取的参数数组
*/
public Map<String, String> converMap(Map<String, String[]> paramMap) {
Map<String, String> rtnMap = new HashMap();
for (String key : paramMap.keySet()) {
rtnMap.put(key, (key)[0]);
}
return rtnMap;
}
}
五、配置⽂件
#登录
zkt.log.operatecode.doLogin=zkt5g-om-doLogin
#退出
zkt.log.operatecode.logout=zkt5g-om-logout
#查看⾸页
zkt.log.operatecode.queryIndex = zkt5g-om-query-index
#商家管理查询
zkt.log.operatecode.shopQueryList = zkt5g-om-shop-query-list
#查看商家详情
zkt.log.operatecode.shopQueryDetail = zkt5g-om-shop-query-detail
#注册商家
zkt.log.operatecode.shopRegister = zkt5g-om-shop-register
#修改商家信息
zkt.log.operatecode.shopUpdate = zkt5g-om-shop-update
#新增签名
zkt.log.operatecode.shopSign = zkt5g-om-shop-sign
六、⼏点要点,就三个要点
注意重点在于切⾯的注⼊@Autowired private Environment environment; 进⾏获取到的值

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