Spring AOP

Spring AOP

  1. 定义注解
package com.ways.mount.annotation;

import java.lang.annotation.*;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataScope {
    /**
     * 部门表的别名
     */
    String deptAlias() default "";

    /**
     * 用户表的别名
     */
    String userAlias() default "";
}
  1. 定义目标方法
package com.ways.mount.service;

import com.ways.mount.annotation.DataScope;
import com.ways.mount.controller.request.UserRequest;
import com.ways.mount.controller.response.UserVO;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    @DataScope(userAlias = "1111", deptAlias = "2222")
    public UserVO getUser(UserRequest request, Integer age) {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("测试一下");
        }
        return new UserVO("张三",request.getSalary());
    }

}

  1. 定义切面
package com.ways.mount.aspect;

import com.ways.mount.annotation.DataScope;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 数据过滤处理
 *
 * @author ruoyi
 */
@Aspect
@Component
@Slf4j
public class DataScopeAspect {

    @Before("@annotation(dataScope)")
    public void doBefore(JoinPoint point, DataScope dataScope) {
        // 在目标方法被调用之前调用通知功能
        log.error("执行了doBefore");

//        String deptAlias = dataScope.deptAlias();
//        log.error("deptAlias = " + deptAlias);
//        MethodSignature signature = (MethodSignature) point.getSignature();
//        Class<?> declaringClass = signature.getMethod().getDeclaringClass();
//        log.error("declaringClass = " + declaringClass);
//        DataScope annotation = signature.getMethod().getAnnotation(DataScope.class);
//        String b = annotation.deptAlias();
//        log.error("b = " + b);
    }

    @After("@annotation(dataScope)")
    public void doAfter(JoinPoint point, DataScope dataScope) {
        // 在目标方法完成之后调用通知,此时不会关心方法的输出是什么
        log.error("执行了doAfter");
    }

    @AfterReturning(value = "@annotation(dataScope)",returning = "result")
    public void doAfterReturning(JoinPoint point, DataScope dataScope, Object result) {
        // 在目标方法成功执行之后调用通知
        log.error("doAfterReturning");
    }

    @AfterThrowing(value = "@annotation(dataScope)", throwing = "exception")
    public void doAfterThrowing(JoinPoint point, DataScope dataScope, Exception exception) {
        // 在目标方法抛出异常后调用通知
        log.error("doAfterThrowing");
    }

    @Around("@annotation(dataScope)")
    public Object doAround(ProceedingJoinPoint pjp, DataScope dataScope) {
        String methodName = pjp.getSignature().getName();
        log.error("==========环绕通知开始了==========");
        Object result = null;
        try{
            // == 前置通知
            log.error("【目标方法】"+methodName);
            // 执行目标方法
            result = pjp.proceed();
            // == 结果通知
            log.error("目标方法返回结果为:"+result);
        }catch (Throwable e) {
            // == 异常通知
            log.error(e.getMessage());
        }
        // == 后置通知
        log.error("后置通知执行");
        return result;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值