主要用于自己定义的代码编写规范的扫描检测,应用启动时,进行检查。
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());
}
});
}
}
233

被折叠的 条评论
为什么被折叠?



