注:一个可以直接拿去用的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