新建一个包,创建注释类
package com.jeeplus.modules.pc.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Administrator
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HlwyyLogger {
// category 和value描述方法的实际作用
String value() default "";
int category() default 0;
}
现在来写HlwyyLogger 注解的实现类
import com.jeeplus.core.persistence.CustomException;
import com.jeeplus.modules.pc.doctormodel.entity.DtDoctorsofPc;
import com.jeeplus.modules.pc.doctormodel.service.DtDoctorsofPcService;
import com.jeeplus.modules.pc.logrecord.entity.DtLogRecordofPc;
import com.jeeplus.modules.pc.logrecord.service.DtLogRecordofPcService;
import com.jeeplus.modules.pc.tools.HttpContextUtils;
import com.jeeplus.modules.pc.tools.IpUtilsofDiy;
import com.jeeplus.modules.pc.tools.JSONUtils;
import com.jeeplus.modules.sys.security.util.JWTUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
/**
* @author Administrator
*/
@Aspect
@Component
public class HlwyyLoggerImpl {
//本地异常日志记录对象
private final static Logger logger = LoggerFactory.getLogger(HlwyyLogger.class);
@Autowired(required = false)
private DtDoctorsofPcService dtDoctorsService;
//注入Service用于把日志保存数据库
@Autowired(required = false)
private DtLogRecordofPcService dtLogRecordofPcService;
//Controller层切点
@Pointcut("@annotation(com.jeeplus.modules.pc.config.HlwyyLogger)")
public void HlwyyLogger() {
}
@Before("HlwyyLogger()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
}
//joinPoint 切点
@Around("HlwyyLogger()")
public Object around(ProceedingJoinPoint point) throws CustomException {
// 执行方法
Object result = null;
this.checkPermission(point);
try {
// 执行方法
result = point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return result;
}
void checkPermission(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
HlwyyLogger syslog = method.getAnnotation(HlwyyLogger.class);
Boolean b = false;
if (syslog != null) {
try {
String params = JSONUtils.beanToJson(syslog.value());
String params1 = JSONUtils.beanToJson(syslog.category());
logger.info("参数为:" + params);
logger.info("参数为:" + params1);
// 获取request(没有token可以不用写)
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
DtLogRecordofPc dtLogRecordofPc=new DtLogRecordofPc();
try{
String username= JWTUtil.getLoginName(request.getHeader("token"));
if(!StringUtils.isBlank(username))
{
DtDoctorsofPc doctor=this.dtDoctorsService.findUniqueByProperty("doctor_name",username);
if(doctor!=null)
{
dtLogRecordofPc.setDoctorId(Integer.parseInt(doctor.getId()));
}
}
}catch (Exception e)
{
}
//需要被存入数据库的字段
dtLogRecordofPc.setCreateTime(new Date());
dtLogRecordofPc.setStatus(1);
dtLogRecordofPc.setContent(params+" url: "+request.getRequestURI());
dtLogRecordofPc.setCategory(syslog.category());
dtLogRecordofPc.setType(2);
dtLogRecordofPc.setUserAgent(request.getHeader("user-agent"));
dtLogRecordofPc.setIp(IpUtilsofDiy.getIpAddr(request));
this.dtLogRecordofPcService.save(dtLogRecordofPc);
} catch (Exception e) {
logger.debug(e.getMessage());
}
}
}
}
注解的实现类(也可以说是切点类)写完,我们就可以对自己的接口添加注解从而将你的操作步骤存入数据库
红色框住的就是我们需要添加的注解
由于我是两个端的日志写在一起所以需要 “category”这个字段来区分,1是后台端,2是用户端。不需要区分的同学只写上注解和你的接口说明(value)就可以啦
感谢观看!!!
下次再见!!!
我是一条小咸鱼