Spring的自定义注解简单实现

1、注解的示例为在方法入参上加后缀

注解代码示例:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WoToken {
    String value() default "好人";
}

 注解解析代码示例:

@Component
@Aspect
public class HelloAspect {
    // 将此类的解析指向注解
    @Pointcut("@annotation(com.annotation.annotationImpl.WoToken)")
    private void cut() {
        //切点
    }
    @Around("cut()&&@annotation(woToken)")
    public Object advice(ProceedingJoinPoint joinPoint, WoToken woToken) throws Throwable {
        // 获取连接点方法运行时的入参列表
        Object[] args = joinPoint.getArgs();
        for (int i= 0;i<args.length;i++) {
            String argValue = (String) args[i];
            String s = argValue + woToken.value();
            args[i] = s;
        }
        //执行
        Object proceed = joinPoint.proceed(args);
        return proceed;
    }

}

 代码示例:https://github.com/Pinshuducha/annotation

 参考:https://blog.csdn.net/message_lx/article/details/77652260

 参考代码:

@Aspect
@Component
@Order(500)
public class DecryptParamInterceptor {
    public static final Log logger = LogFactory.getLog(DecryptParamInterceptor.class);

    @Pointcut("@annotation(com.haiercash.common.util.annotation.DecryptParam)")
    private void decryptParamCut() {
        //切点
    }

    @Around(value = "decryptParamCut()")
    public Object dectyptParam(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        if (method == null) {
            return joinPoint.proceed();
        }
        DecryptParam annotation = methodSignature.getMethod().getAnnotation(DecryptParam.class);
        if (annotation.value().length == 0) {
            return joinPoint.proceed();
        }

        String[] decryptParams = annotation.value();
        List<String> decryptParamsList = Arrays.asList(decryptParams);

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String[] parameterNames = signature.getParameterNames();
        Object[] args = joinPoint.getArgs();
        boolean isSimpleArg = false;
        
        for(int i = 0; i < args.length; i++) {
            if (decryptParamsList.contains(parameterNames[i]) && args[i] instanceof String) {
                String argValue = (String) args[i];
                String decryptValue = EncryptUtil.simpleDecrypt(argValue);
                args[i] = decryptValue;
                logger.info(String.format("参数【%s】原值【%s】解密为【%s】", parameterNames[i], argValue, decryptValue));
                isSimpleArg = true;
            }
        }
        if (isSimpleArg) {
            return joinPoint.proceed(args);
        }

        // 复杂参数,只对第一个对象做处理
        for(int i = 0; i < args.length; i++) {
            if (args[i] == null) {
                continue;
            }
            Map<String, Object> map = BeanUtils.beanToMap(args[i]);
            decryptParamsList.stream().filter(decryptParam -> !StringUtils.isEmpty(map.get(decryptParam)) && map.get(decryptParam) instanceof String)
                    .forEach(decryptParam -> {
                        String argValue = (String) map.get(decryptParam);
                        String decryptValue = EncryptUtil.simpleDecrypt(argValue);
                        map.put(decryptParam, decryptValue);
                        logger.info(String.format("参数【%s】原值【%s】解密为【%s】", decryptParam, argValue, decryptValue));
                    });
            
            Class<?> aClass = args[i].getClass();
            args[i] = BeanUtils.mapToBean(map, aClass);
        }
        return joinPoint.proceed(args);
    }
}
原文地址:https://www.cnblogs.com/lu51211314/p/10375530.html