spring boot中通过开发jar包,检查目标应用的注解的非法使用
来源:互联网 发布:法国达索软件 编辑:程序博客网 时间:2024/05/24 05:45
背景
在spring boot工程中,@FeignClient和@RequestMapping可能被错误得加到同一个方法上,当用户这么使用的情况下,如何能够在程序启动的过程中警告用户并抛出异常呢?本文将给出这个问题的解决方法。
思路
- 通过提供一个jar包,任何引入该jar包的应用在启动过程中自动执行注解使用检查
- jar包需要添加一个Listener,监听应用启动的过程,执行检查工作
步骤
- 实现注解检查Listener
- 启动spring boot自动配置
- 目标应用启动@EnableAutoCongiguration自动配置
代码
Listener定义
@Component@ConditionalOnClass(FeignClient.class)public class AnnotationCheckListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired public ApplicationContext applicationContext; @Override public void onApplicationEvent(ContextRefreshedEvent event) { String[] beanNamesForAnnotation = applicationContext.getBeanNamesForAnnotation(RequestMapping.class); Arrays.stream(beanNamesForAnnotation) .filter(beanName -> applicationContext.findAnnotationOnBean(beanName, FeignClient.class) != null) .forEach(beanName -> throwAnnotationConfigurationException(beanName)); } static void throwAnnotationConfigurationException(String beanName) { throw new AnnotationConfigurationException("Cannot have both @RequestMapping and @FeignClient on " + beanName); }}配置spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.swagger.starter.AnnotationCheckListener
总结
SpringFactoriesLoader
loads and instantiates factories of a given type from "META-INF/spring.factories" files which may be present in multiple JAR files in the classpath. The
SpringFactoriesLoaderspring.factories
file must be inProperties
format, where the key is the fully qualified name of the interface or abstract class, and the value is a comma-separated list of implementation class names.- @EnableAutoCongiguration Spring Boot提供的
@EnableAutoCongiguration
功能很强大,所有的配置似乎都被包含进来而无需开发者显式声明。EnableAutoConfigurationImportSelector
使用的是spring-core
模块中的SpringFactoriesLoader#loadFactoryNames()
方法,它的作用是在类路径上扫描并加载META-INF/spring.factories
文件中定义的类。
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Import({ EnableAutoConfigurationImportSelector.class, AutoConfigurationPackages.Registrar.class })public @interface EnableAutoConfiguration { /** * Exclude specific auto-configuration classes such that they will never be applied. */ Class<?>[] exclude() default {};}
备选
@Component@ConditionalOnClass(FeignClient.class)@Slf4jpublic class FeignClientBeanProcessor implements BeanPostProcessor{ @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { return null; } @Override public Object postProcessAfterInitialization(Object bean, String s) throws BeansException { final Class<?> beanClass = bean.getClass(); if(beanClass.isAnnotationPresent(RequestMapping.class) && beanClass.isAnnotationPresent(FeignClient.class)) { log.error("RequestMapping and FeignClient annotation could not be added to the same class."); return new BeanCreationException("RequestMapping and FeignClient annotation could not be added to the same " + "class"); } return null; }}
0 0
- spring boot中通过开发jar包,检查目标应用的注解的非法使用
- spring boot 打成jar包后 通过命令行传入的参数 3中实现方式
- 解决spring boot的jar包通过java -jar命令运行时提示"缺少主清单..."
- 通过loosejar清理应用中冗余的jar包
- spring boot通过jar包启动时,配置文件的加载顺序
- Spring Boot项目中自定义注解的使用
- Spring Boot中缓存注解的配置与使用
- Spring Boot项目中自定义注解的使用
- Spring Boot中几个常用的注解
- Spring boot 中常用的注解
- spring boot 的常用注解使用 总结
- spring boot 的常用注解使用 总结
- Spring Aop开发过程中可能出现的异常(通过这些异常可以知道需要什么Jar包)
- spring boot 的 pom.xml引入自定义的jar包
- Spring所依赖的AspectJ的jar包下载/Spring-aop注解jar包下载
- spring Boot中Maven的下载安装和自定义jar包处理
- spring boot加载第三方jar包的配置文件
- spring boot解决第三方jar包依赖的问题
- Android 第一次使用阿里云存储存储文件
- igmp协议
- linux常用指令记录
- HDU 1527 取石子游戏 威佐夫博弈裸题
- android:descendantFocusability用法简析
- spring boot中通过开发jar包,检查目标应用的注解的非法使用
- 取关键字下第2行
- 监听输入法软键盘回车键
- nginx 水印
- stat函数及ls功能
- c3p0报空指针错误
- 机器学习与数据挖掘的学习路线图
- 时间是怎么样挤出来的?
- Hdu 5769 Substrings (后缀数组)