自定义注解使用规范和方法定义规范检测

主要用于自己定义的代码编写规范的扫描检测,应用启动时,进行检查。

1、检测指定注解类中的方法上,是否带必写的注解;

2、检测指定注解类中的方法,出参和入参是否为指定类型。

@Configuration
@Slf4j
public class AnnotationCheckConfig {

    @Autowired
    private SpringBeanFactory springBeanFactory;

    @PostConstruct
    public void checkAnnotation() {
        Map<String, Object> controllerBeans = SpringBeanFactory.getBeanListByAnnotationClass(Controller.class);
        controllerBeans.entrySet().forEach(entry -> {
            Class<?> clazz = entry.getValue().getClass();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                Annotation[] annotations = method.getDeclaredAnnotations();
                for (Annotation annotation : annotations) {
                    String annotationName = annotation.annotationType().getName();
                    log.info("{}类上有方法{},该方法上有注解annotationName={}",entry.getKey(), method.getName(), annotationName);
                }
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    String parameterTypeName = parameterType.getName();
                    log.info("该方法的入参类型:{}", parameterTypeName);
                }
                Class<?> returnType = method.getReturnType();
                log.info("该方法的出参类型:{}", returnType.getName());
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值