Spring AOP应用之操作日志管理并存储数据库

首先需要了解一下Spring实现AOP的4种方法,参考下面这篇博文:

Spring实现AOP的4种方式
https://blog.csdn.net/udbnny/article/details/5870076

本文将采用第2种方法,即@AspectJ注解驱动的切面

当然还有AOP中的5种通知方式也务必了解,参考如下博文:

https://blog.csdn.net/sinat_28978689/article/details/62215513
spring知识六——AOP五大通知

本文采用的是@Around,即环绕通知,虽然是杀鸡用牛刀,但杀的痛快又有何妨。

明白了以上知识点,就可以双手搭上键盘飞舞起来了,直接上代码:


1.创建一个注解类,取名为ArchivesLog

package com.dcms.admin.annotation;

import java.lang.annotation.*;

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ArchivesLog {
   

    String message() default "";

}

2.新建类ArchivesLogAspect

package com.dcms.admin.annotation;


import com.dcms.admin.bean.Log;
import com.dcms.admin.bean.User;
import com.dcms.admin.service.ILogService;
import com.dcms.util.IPUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Aspect
@Component
public class ArchivesLogAspect {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpSession session;
    @Autowired
    private ILogService logService;

    private static Log log = new Log();

    @Around("within(com.dcms..*) && @annotation(archivesLog)")
    public Object around(ProceedingJoinPoint pjd, ArchivesLog archivesLog) throws Throwable {
        log.setLogId(null);
        log.setCtime(new Date());
        long startTime=System.currentTimeMillis();

        //类名
        String className = pjd.g
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值