java-自定义方法注解实现AOP面向切面编程

首先定义注解类
import java.lang.annotation.*;

/**
* @description: 保存或者修改字典权限判断注解
* @fileName: CommDictValidateAspect.java
* @author: Sure
* @createAt: 2021/12/13/013 16:14
* @updateBy: Sure
* @remark: 
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CommDictValidate {
    /**
     * 权限不足的默认提示信息
     */
    public static final String MESSAGE = "非超级管理员无法维护公共字典";

    /**
     * 无权限时的提示信息
     *
     * @return
     */
    String message() default MESSAGE;
}

定义切面类

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.data.annotation.Id;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static 

/**
* @description: 保存字典权限控制切面
* @fileName: CommDictValidateAspect.java
* @author: Sure
* @createAt: 2021/12/13/013 16:16
* @updateBy: Sure
* @remark: 
* */
@Component
//@Configuration
@Aspect
@Slf4j
public class CommDictValidateAspect {

//改成自己注解的地址
@Pointcut("@annotation(com.**.**.base.common.server.common.annotation.CommDictValidate)")
    public void doAspect() {
        log.warn("进入字典保存权限校验注解:{}的处理切点", CommDictValidate.class.getName());
    }

    /**
     * 处理 CommDictValidate 注解的切面
    * @Param:  连接点
    * @return: 返回值
    */
    @Around("doAspect()")
    public Object around(ProceedingJoinPoint point){
        try {
            Long id=null;
            Field field = null;
            LoginUserInfo loginUserInfo= LoginUserContext.getLoginUserInfo();
            log.warn("进入字典保存权限校验注解:{}的处理切面", CommDictValidate.class.getName());
            Object[] args = point.getArgs();
            Object arg = args[0];
            //自己的逻辑判断
            if (ObjectUtils.isNotEmpty(arg)){
                JSONObject jsonObject=JSON.parseObject(JSON.toJSONString(arg));
                Long hospitalId = jsonObject.getLong("hospitalId");
 
                    });
                }
            }
            return null;
        }
        catch (Throwable throwable){
            log.error("字典保存权限校验异常",throwable);
            throw BusinessException.build(DictSaveOrUpdateErrorCodeEnum.PERMISSION_ASPECT_EX,throwable.getMessage());
        }

    }

    /**
     * 获取注解中的参数
     *
     * @param joinPoint 连接点
     * @return 注解元信息
     */
    private String getAnnotationMessage(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        CommDictValidate annotation = method.getAnnotation(CommDictValidate.class);
        return annotation.message();
    }
}

最后放在方法的注释上即可生效

在这里插入图片描述


版权声明:本文为GodSure0914原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。