AOP日志
1,导入Aspect的依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
2,因为都是基于springboot的项目,所以不再xml里面配置aop,此时需要些aop自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation{
String operationName();
String operationType();
}
3,需要在哪个方法上面开启AOP日志,就打上自定义的LogAnnotation注解,如:
@LogAnnotation(operationType = "/login",operationName = "用户登录")
@RequestMapping("/login")
public String login(String username,String password){
Subject subject = SecurityUtils.getSubject();
if (!subject.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
}
return "success";
}
4,在具体的类里面开启AOP日志
public class LogAspect {
@Pointcut("execution(* persion.david..controller.*.*(..))")
public void pointCut() {
}
public void before(JoinPoint joinPoint) throws Exception {
String name = joinPoint.getTarget().getClass().getName();
String name1 = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Class<?> clazz = Class.forName(name);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(name1)) {
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null) {
String operationName = logAnnotation.operationName();
String operationType = logAnnotation.operationType();
SysAdmin sysAdmin = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
System.out.println(operationName + "被执行了(" + operationType + "),参数是:" + Arrays.toString(args) + (sysAdmin == null ? "" : "执行的用户是:==>" + sysAdmin.getName()));
}
}
}
}
@AfterReturning(value = "pointCut()", returning = "o")
public void after(JoinPoint joinPoint, Object o) throws Exception {
String name = joinPoint.getTarget().getClass().getName();
String name1 = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Class<?> clazz = Class.forName(name);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(name1)) {
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null) {
String operationName = logAnnotation.operationName();
String operationType = logAnnotation.operationType();
SysAdmin sysAdmin = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
System.out.println(operationName + "被执行了(" + operationType + "),参数是:" + Arrays.toString(args) + (sysAdmin == null ? "" : "执行的用户是:==>" + sysAdmin.getName()));
}
}
}
}
@AfterThrowing(pointcut = "pointCut()",throwing = "t")
public void afterThrowing(JoinPoint joinPoint,Throwable t) throws Exception {
String name = joinPoint.getTarget().getClass().getName();
String name1 = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Class<?> clazz = Class.forName(name);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(name1)) {
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null) {
String operationName = logAnnotation.operationName();
String operationType = logAnnotation.operationType();
SysAdmin sysAdmin = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
System.out.println(operationName + "被执行了(" + operationType + "),参数是:" + Arrays.toString(args) + (sysAdmin == null ? "" : "执行的用户是:==>" + sysAdmin.getName())+"结果失败了");
}
}
}
}
}