SpringBoot整合Aspect实现AOP和注解方式的日志和编程

1.引入依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

2.编写注解类


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;

/**
 * 注解本身是没有功能的,就和xml一样.
 * 注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置.
 * @author Administrator
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AdminLog {

  /*
  type 操作类型:新增,修改,删除,其他
  module 操作的功能模块
   */
  String type();
  String module();

}

3.编写切面,切点,各种通知


import xxx.service.AdminLogService;
import java.lang.reflect.Method;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect //声明一个切面
@Component
@AllArgsConstructor
public class LogAspect {

  AdminLogService adminLogService;

  //声明一个切点
  @Pointcut("@annotation(xxx.aop.AdminLog)")
  public void annotationPoinCut() {
  }

  /**
   * 基于注解的拦截
   */
  //声明一个建言
  @After("annotationPoinCut()")
  public void after(JoinPoint joinPoint) {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
        .getRequestAttributes()).getRequest();
    HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder
        .getRequestAttributes()).getResponse();
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    //方法入参
    Object[] args = joinPoint.getArgs();
    //获取目标的类
    Object target = joinPoint.getTarget();
    //获取方法上的注解的实例,就可以拿到里面的信息

    //写入数据库
    AdminLog action = method.getAnnotation(AdminLog.class);
    xxx.entity.AdminLog adminLog
        = new xxx.entity.AdminLog();
    adminLog.setName("default");
    adminLog.setType(action.type());
    adminLog.setModule(action.module());
    adminLog.setDetail("detail");
    adminLog.setCreation(new Date());
    try {
      adminLogService.insert(adminLog);
    } catch (Exception e) {
      e.printStackTrace();
    }
    //通过反射可以获得注解上的属性,然后做日志记录相关的操作,下面的相同.
    System.out.println("注解式拦截: " + action.type());
  }

  @AfterReturning(value = "annotationPoinCut()",returning = "methodResult")
  public void afterReturning(JoinPoint joinPoint, Object methodResult) {
    MethodSignature ms = (MethodSignature) joinPoint.getSignature();
    Method method = ms.getMethod();
    System.out.println("请求方法为:" + method.getName());
    System.out.println("请求返回内容为:" + methodResult.toString());
  }

  /**
   * 基于方法的拦截
   */
  @Before("annotationPoinCut()")
  public void before(JoinPoint joinPoint) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    System.out.println("方法规则拦截: " + method.getName());
  }

//  /**
//   * 基于方法的拦截
//   * @param joinPoint
//   */
//  @Before("execution(* com.hgh.springboot_1.test1_3_2.DemoMethodServices.*(..))")
//  public void before(JoinPoint joinPoint){
//    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//    Method method = signature.getMethod();
//    System.out.println("方法规则拦截: " + method.getName());
//  }

}

4.测试方法

  /**
   * http://127.0.0.1:8080/game/aopTest
   */
  TestService testService;
  @RequestMapping("/aopTest")
  public Result aopTest() {
    String s = testService.aopTest("value1","value2");
    return RespResult.success(s);
  }

service

import xxx.aop.AdminLog;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@AllArgsConstructor
public class TestService {

  @AdminLog(type = "新增",module = "测试模块")
  public String aopTest(String v1,String v2) {
    return "success";
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值