Spring的aop切面编程技术切到自定义注解上,针对不同注解标志进行参数解析,记录日志。
依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
|
日志实体类
1 2 3 4 5 6 7 8 9 10 11
| public class Record implements Serializable { private Long id;
private String userName;
private String operation;
private Date createDate; }
|
自定义注解类
1 2 3 4 5 6 7 8 9 10 11
| import java.lang.annotation.*;
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyRecord { String value() default ""; }
|
创建aop切面实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import com.alibaba.fastjson.JSON; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Date;
@Aspect @Component public class RecordAspect {
@Autowired private RecordService recordService;
@Pointcut("@annotation( com.example.record.MyRecord)") public void logPoinCut() { }
@AfterReturning("logPoinCut()") public void saveRecord(JoinPoint joinPoint) { System.out.println("切面。。。。。"); Record record = new Record();
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod();
MyRecord myRecord = method.getAnnotation(MyRecord.class); if (myRecord != null) { String value = myRecord.value(); sysLog.setOperation(value); }
record.setCreateDate(new Date()); record.setUsername(ShiroUtils.getUserEntity().getUsername());
recordService.save(record); }
}
|
在需要监控的方法上添加 aop的自定义注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @RestController @RequestMapping("/sys/menu") public class SysMenuController extends AbstractController {
@Autowired private SysMenuService sysMenuService;
@MyRecord(value = "删除菜单记录") @PostMapping("/del") public R deleteBatch(@RequestBody Long[] menuIds) { for (Long menuId : menuIds) { if (menuId <= 31) { return R.error("系统菜单,不能删除"); } } sysMenuService.deleteBatch(menuIds);
return R.ok("删除成功"); } }
|