Java实现AOP切面记录日志实例

本文提供了一个使用SpringMVC框架的AOP切面实例,用于记录系统操作日志,包括管理员查看账户操作记录、时间、账户名称和IP。主要步骤包括创建自定义注解、日志实体Bean、监听注解的实现、配置@AspectJ支持和业务Controller引用。文章未深入探讨AOP原理,适合初学者快速上手。
摘要由CSDN通过智能技术生成

注:一个可以直接拿去用的aop切面保存系统操作日志的实例,应用框架为SpringMVC。

需求描述:
管理员要在系统中能看到每个账户的操作记录、时间、账户名称、ip。

实现步骤:
1、实现自定义注解MethodLog
2、建日志实体Bean
3、实现监听注解,调用切面实现类
4、配置启动对@AspectJ注解的支持及监听类
5、业务Controller引用

具体实现:
1、建一个接口类,内容如下

package com.xx.xxx.utils;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLog {
    String remark() default "";  // 自定义的操作描述属性
    String operType() default "0";  //  自定义的操作类型
}

2、建日志实体Bean

package com.xx.xxx.entity;

public class Syslog {

    private String ipAddress;
    private String loginName;
    private String methodName;
    private String methodRemark;
    private String operation;
    private String operationDate;

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getMethodRemark() {
        return methodRemark;
    }

    public void setMethodRemark(String methodRemark) {
        this.methodRemark = methodRemark;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.ope
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值