AOP 自定义注解(日志注解)

前提引入aop依赖完成(具体不介绍)

实操

在这里插入图片描述

创建自定义接口类 Loggable

import java.lang.annotation.*;

/**
 * @Author hw
 * @Date 2024/4/7 9:22
 * @PackageName:xxxxxxxxx
 * @ClassName: Loggable
 * @Description: 登录日志
 * @Version 1.0
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface Loggable {
    /**
     * 说明
     */
    String operatorDetails() default "";

    /**
     * 操作
     */
    String operation();


    /**
     * 操作位置
     */
    String operationAddress();

    /**
     * 操作模块
     */
    String operatorModule();
}

定义切面类

/**
 * @Author hw
 * @Date 2024/3/1 9:28
 * @PackageName:xxxxxxxxx
 * @ClassName: LoggableAspect
 * @Description: 登录模块的aop
 * @Version 1.0
 */
@Aspect
@Slf4j
@Component
public class LoggableAspect {

    @Autowired
    private EipSysLogLoginServiceImpl eipSysLogLoginService;

    @Pointcut("@annotation(com.redxun.eip.annotation.Loggable)")
    public void loggableAspect() {
    }


    @AfterReturning("execution(* *.*(..)) && @annotation(loggable)")
    public void logAfterReturningMethod(JoinPoint joinPoint, Loggable loggable) throws Exception{
        log.info("==========执行业务操作日志留痕===============");
        EipSysLogLogin eipSysLogLogin = new EipSysLogLogin();
        // 操作  操作位置  操作内容 操作模块
        eipSysLogLogin.setOperator(loggable.operation());
        eipSysLogLogin.setOperatorAddress(loggable.operationAddress());
        eipSysLogLogin.setOperatorDetails(loggable.operatorDetails());
        eipSysLogLogin.setOperatorModule(loggable.operatorModule());
        eipSysLogLoginService.saveLoginLog(joinPoint,eipSysLogLogin);
    }
}

Service层写具体的代码

    /**
     * description:
     *          保存登录日志操作
     * @author: hw
     * @since: 1.0.0
     * @date: 2024/4/7 10:35
     * @Param joinPoint:
     * @Param eipSysLogLogin:
     * @return: void
    */
    public void saveLoginLog(JoinPoint joinPoint, EipSysLogLogin eipSysLogLogin) {

        EipEhrEmp eipEhrEmp = (EipEhrEmp) redisTemplate.opsForHash().get(EipConstant.USER_PERMISSION, SecurityGetUserUtils.getUserByWorkId());
        if (ObjectUtil.isNotEmpty(eipEhrEmp)) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            // 主键
            eipSysLogLogin.setId(IdGenerator.getIdStr());
            // 记录时间
            eipSysLogLogin.setRecordTime(new Date());
            // 操作人
            eipSysLogLogin.setOperatorWorkId(eipEhrEmp.getWorkId());
            eipSysLogLogin.setOperatorName(eipEhrEmp.getName());
            // 创建时间、更新时间
            eipSysLogLogin.setCreateTime(new Date());
            eipSysLogLogin.setUpdateTime(new Date());
            // 创建人 更新人
            eipSysLogLogin.setCreateBy(eipEhrEmp.getWorkId());
            eipSysLogLogin.setUpdateBy(eipEhrEmp.getWorkId());
            eipSysLogLogin.setCreateByName(eipEhrEmp.getName());
            eipSysLogLogin.setUpdateByName(eipEhrEmp.getName());

            // 逻辑删除(1-有效 0-删除)
            eipSysLogLogin.setDelete(1);

            // ip地址
            String ipAddr = IpUtil.getIpAddr(request);
            eipSysLogLogin.setIp(ipAddr);
            // MAC 地址
            eipSysLogLogin.setMac(MacAddressUtils.getMacAddressByIp(ipAddr));
            // 主机名
            eipSysLogLogin.setHostname(request.getHeader("Host"));
            // 登录地区  需要具体的api
            eipSysLogLogin.setLoginAddress(request.getLocale().toString());
            // 浏览器
            eipSysLogLogin.setBrowser(BrowserUtils.getBrowser(request));
            // 操作系统
            eipSysLogLogin.setOperatorSystem(OperatingSystemUtils.getOperatingSystem(request));

            // 创建部门,公司
            eipSysLogLogin.setCreateDepId(eipEhrEmp.getDeptId());
            eipSysLogLogin.setCompanyId(eipEhrEmp.getCompanyId());

            // 获取当年的年份
            LocalDate currentDate = LocalDate.now();
            int currentYear = currentDate.getYear();
            eipSysLogLoginMapper.saveEipSysLogLogin(eipSysLogLogin,currentYear);
        }
    }
  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP是一种利用代理机制实现面向切面编程的框架。它可以通过自定义注解来实现一些特定的逻辑。在Spring AOP中,我们可以通过在自定义注解上添加相关的切点表达式和通知方法来实现对目标方法的增强。这样可以实现一些横切关注点的统一处理,如记录系统日志、权限校验等。通过使用Spring AOP自定义注解,我们可以更加灵活地对代码进行管理和维护。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring aop 自定义注解保存操作日志到mysql数据库 源码](https://download.csdn.net/download/y_h_d/48993109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Spring AOP 自定义注解的实现代码](https://download.csdn.net/download/weixin_38714637/12781779)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [spring AOP自定义注解方式实现日志管理的实例讲解](https://download.csdn.net/download/weixin_38552536/12764634)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值