aop日志的实现

AOP是spring的核心之一。用AOP来实现用户操作日志管理非常合适。 
学东西应该先具体再抽象。下面直接上代码

  • pom.xml
<!-- 面向切面的包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.4</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • SpringServlet.xml
    <!-- aop 切面 -->
    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="com.kylin.aep.aop"></context:component-scan>
    <!--  强制使用cglib代理,如果不设置,将默认使用jdk的代理,但是jdk的代理是基于接口的 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • ControllerAspect.java

@Aspect
@Component
/*
You may register aspect classes as regular beans in your Spring XML configuration, or autodetect them through classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate @Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring's component scanner).
*/
public class ControllerAspect {
    @Autowired
    @Qualifier("SystemManageServiceImpl")
    SystemManageService systemManageServiceImpl;

    private static final Logger log = Logger.getLogger(ControllerAspect.class);
    // 方法执行之前被调用执行
    /* 定义切入点 
       (配置在com.mycomp.myproj.controller包下所有后缀为Controller的类的所有方法在调用之前都会被拦截)
       用户的所有操作,最终都是页面请求controller中的方法。
       controller中方法执行之前会执行下面的方法,在这个时候写入日志。
    */
    @Before("execution(* com.mycomp.myproj.controller.*Controller.*(..))")
    public void permissionCheck(JoinPoint point) throws Throwable {
        // 获取调用的controller的类名
        String className = point.getSignature().getDeclaringType().getSimpleName();
        // 获取调用的controller类中的方法名
        String methodName = point.getSignature().getName();
        String operation = className + "." + methodName;
        //前端调用的方法和传入的参照都写入log
        log.info("控制器" + className + "." + methodName + "入参:" + Arrays.toString(point.getArgs()));
        // 获取方法中的参数
        Object[] args = point.getArgs();
        if (args.length > 0 && args[args.length - 1] instanceof HttpSession) {
            //参数列表的最后一个是httpsession
            HttpSession session = (HttpSession) args[args.length - 1];
            //从session中获取当前登录用户
            User user = (User) session.getAttribute("user");
            if (user != null) {
                //操作日志写入日志表
                //后面写入数据库之前会对operation做一个中文转义,毕竟是给用户看的。
                systemManageServiceImpl.saveSystemLog(user, operation, 0);
            }
            else {
                log.info("未知用户进行" + operation + "操作!");
            }
        }
    }
}转载地址:https://blog.csdn.net/Erictju/article/details/78673283
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值