源码
除了需要导入相关的spring
的包,还要添加三个jar
包。aopalliance
、aspectjrt
、aspectjweaver
。在这里我导入的是aopalliance-1.0.jar
,aspectjrt-1.9.1.jar
,aspectjweaver-1.9.1.jar
import java.lang.annotation.*;
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperateLog {
//操作模块
String operatorModule() default "";
}
日志实体类,用于记录修改的日志
package com.howen.entity;
public class LogEntity {
private String operator;
private String date;
private String operated;
private Object changeBefore;
private Object changeAfter;
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getOperated() {
return operated;
}
public void setOperated(String operated) {
this.operated = operated;
}
public Object getChangeBefore() {
return changeBefore;
}
public void setChangeBefore(Object changeBefore) {
this.changeBefore = changeBefore;
}
public Object getChangeAfter() {
return changeAfter;
}
public void setChangeAfter(Object changeAfter) {
this.changeAfter = changeAfter;
}
@Override
public String toString() {
return "LogEntity{" +
"operator='" + operator + '\'' +
", date='" + date + '\'' +
", operated='" + operated + '\'' +
", changeBefore=" + changeBefore +
", changeAfter=" + changeAfter +
'}';
}
}
学生实体类
package com.howen.entity;
public class Student {
private int id;
private String name;
private int age;
private String sex;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
controller
层package com.howen.controller;
import com.howen.config.OperateLog;
import com.howen.db.DBConfig;
import com.howen.entity.Student;
import com.howen.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class StudentController {
@Autowired
StudentService studentService;
@OperateLog(operatorModule = "修改" )
@RequestMapping(value = "/change")
public String change(int id, String address,int age){
boolean isChanged = StudentService.change(id,address,age);
return "success";
}
@OperateLog(operatorModule = "添加")
@RequestMapping(value = "/add")
public String add(int id,String name,String address,int age,String sex){
Student student = new Student();
student.setAge(age);
student.setAddress(address);
student.setId(id);
student.setName(name);
student.setSex(sex);
System.out.println(student.toString());
DBConfig.studentDB.put(id,student);
return "success";
}
}
service
层package com.howen.controller;
import com.howen.config.OperateLog;
import com.howen.db.DBConfig;
import com.howen.entity.Student;
import com.howen.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class StudentController {
@Autowired
StudentService studentService;
@OperateLog(operatorModule = "修改" )
@RequestMapping(value = "/change")
public String change(int id, String address,int age){
boolean isChanged = StudentService.change(id,address,age);
return "success";
}
@OperateLog(operatorModule = "添加")
@RequestMapping(value = "/add")
public String add(int id,String name,String address,int age,String sex){
Student student = new Student();
student.setAge(age);
student.setAddress(address);
student.setId(id);
student.setName(name);
student.setSex(sex);
System.out.println(student.toString());
DBConfig.studentDB.put(id,student);
return "success";
}
}
实际参数,可以通过debug查看相应的参数,从而获取你所需要的
package com.howen.controller;
import com.howen.config.OperateLog;
import com.howen.entity.LogEntity;
import com.howen.service.StudentService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 这里不增加Component注解的话,该类是无法进行扫描的
*/
@Aspect
@Component
public class LogAopAtion {
/**
* 可以将日志信息存入数据库
*/
@Resource
StudentService studentService;
@Pointcut("execution(* com.howen.controller.StudentController.*(..))")
private void controllerAspect(){}
@Around("controllerAspect()")
public Object doAdd(ProceedingJoinPoint pjp) throws Throwable {
LogEntity logEntity = new LogEntity();
logEntity.setOperator("从系统获取");
System.out.println("------------------------------------------");
String time = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
logEntity.setDate(time);
//获取拦截的实体类,就是正在执行的controller
Object target = pjp.getTarget();
//拦截的方法名称。正在执行的方法
String methodName = pjp.getSignature().getName();
//拦截的方法参数
Object [] args = pjp.getArgs();
//拦截的放参数类型
Signature sig = pjp.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)){
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature)sig;
//获取所有的参数信息
Class [] parameterTypes = msig.getMethod().getParameterTypes();
Object object = null;
//获得拦截的方法
Method method = null;
try{
method = target.getClass().getMethod(methodName,parameterTypes);
}catch (NoSuchMethodException e1){
e1.printStackTrace();
}catch (SecurityException e2){
e2.printStackTrace();
}
if (null!=method){
if (method.isAnnotationPresent(OperateLog.class)){
//利用反射获取注解的信息
OperateLog operateLog = method.getAnnotation(OperateLog.class);
String operateModule = operateLog.operatorModule();
System.out.println(operateModule);
}else{
object = pjp.proceed();
}
}else{
object = pjp.proceed();
}
return object;
}
}
这里就没有使用数据库连接,而是使用map进行替代
package com.howen.db;
import com.howen.entity.Student;
import java.util.HashMap;
import java.util.Map;
public class DBConfig {
public static Map studentDB = new HashMap<>();
}
applicationContext.xml
需要配置扫描包、注解驱动、以及切面自动代理
<context:component-scan base-package="com.howen"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<aop:aspectj-autoproxy proxy-target-class="true"/>