Spring Boot 中使用Spring Aop实现日志记录功能

一、创建日志注解

1、日志类型枚举类

import lombok.Getter;

/**
* 日志类型枚举类
*/
@Getter
public enum LogType {
/**
* 登录
*/
login("LoginLog");

/**
* 实现类名称
*/
private final String implClassName;

LogType(String implClassName) {
this.implClassName = implClassName;
}
}

2、自定义日志注解

import java.lang.annotation.*;
import com.baige.enums.LogType;

/**
* 自定义日志注解
*
* 注解说明:
* @Documented:文档注解
* @Retention(RetentionPolicy.RUNTIME):生命周期注解,注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
* @Target({ ElementType.PARAMETER, ElementType.METHOD }):@Target说明了Annotation所修饰的对象范围,ElementType.TYPE:接口、类、枚举、注解,ElementType.METHOD:方法
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.METHOD })
public @interface Log {
/**
* 登录类型
*/
LogType logType();
}

二、利用AOP实现日志切面统一处理

1、日志实现类创建工厂

import com.baige.enums.LogType;
import com.baige.util.SpringContextHolder;
import org.springframework.context.ApplicationContext;

/**
* 日志实现类创建工厂
*/
public class LogFactory {

public static ApplicationContext context;

public static IUserLog getInstance(LogType logType) {
return (IUserLog) SpringContextHolder.getBean(logType.getImplClassName());
}
}

2、切面类

import lombok.extern.slf4j.Slf4j;
import com.baige.aop.log.IUserLog;
import com.baige.aop.annotation.Log;
import com.baige.aop.log.LogFactory;

import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LogAspect {
/**
* 定义切入点,切入点为com.baige.controller.AopController中的所有函数
* 通过@Pointcut注解声明频繁使用的切点表达式
*/
@Pointcut("execution(public * com.baige.controller.UserController.*(..)))")
public void pointcut() {
}

@Around("pointcut()")
public void doAround(ProceedingJoinPoint pjp) {
try {
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Log log = methodSignature.getMethod().getAnnotation(Log.class);

IUserLog userLog = LogFactory.getInstance(log.logType());
userLog.addLog(pjp, log);

} catch (Throwable throwable) {
log.info("日志记录异常,不影响正常业务", throwable);
}
}
}

三、创建日志实现类

1、添加日志接口

import com.baige.aop.annotation.Log;
import org.aspectj.lang.ProceedingJoinPoint;

import java.net.UnknownHostException;

public interface IUserLog {
/**
* 添加日志
*/
int addLog(ProceedingJoinPoint pjp, Log log) throws UnknownHostException;
}

2、添加日志接口实现类

import java.util.Date;
import java.net.InetAddress;
import java.net.UnknownHostException;

import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import com.baige.domain.User;
import com.baige.domain.UserLog;
import com.baige.aop.log.IUserLog;
import com.baige.mapper.UserMapper;
import com.baige.aop.annotation.Log;
import com.baige.mapper.UserLogMapper;

@AllArgsConstructor
@Component("LoginLog")
public class LoginLog implements IUserLog {

private final UserMapper userMapper;
private final UserLogMapper userLogMapper;

@Override
public int addLog(ProceedingJoinPoint pjp, Log log) throws UnknownHostException {
UserLog userLog = new UserLog();
Object[] params = pjp.getArgs();

if (params.length > 0) {
User param = (User) params[0];
User user = userMapper.getOne(param.getLoginName(), param.getLoginPassword());
if (user != null) {
userLog.setUserId(user.getId());
userLog.setContent("获取用户信息成功");
} else {
userLog.setContent("未获取到用户信息");
}
}

userLog.setIpAddress(InetAddress.getLocalHost().getHostAddress());
userLog.setCreateTime(new Date());

return userLogMapper.insert(userLog);
}
}

四、controller

package com.baige.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import com.baige.domain.User;
import com.baige.enums.LogType;
import com.baige.aop.annotation.Log;
import com.baige.service.UserService;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

private final UserService userService;

@PostMapping("/login")
@Log(logType = LogType.login)
public String login(@RequestBody User params) {
User user = userService.login(params.getLoginName(), params.getLoginPassword());
return user != null ? "登录成功" : "登录失败";
}
}

当前标题:Spring Boot 中使用Spring Aop实现日志记录功能
地址分享:http://www.shufengxianlan.com/qtweb/news8/410808.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联