这篇文章主要介绍“ConditionalOnProperty配置swagger不生效如何解决”,在日常操作中,相信很多人在ConditionalOnProperty配置swagger不生效如何解决问题上存在疑惑,小编查阅了各式资料,整理出简
这篇文章主要介绍“ConditionalOnProperty配置swagger不生效如何解决”,在日常操作中,相信很多人在ConditionalOnProperty配置swagger不生效如何解决问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ConditionalOnProperty配置swagger不生效如何解决”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
在正式环境准备关闭Swagger,添加了ConditionalOnProperty注解进行配置,结果发现怎么也无法关闭swagger。
@EnableSwagger2在多个地方进行了配置,如下:
1)应用程序处进行了配置
@SpringCloudApplication@EnableSwagger2 //在应用程序处进行了配置public class SysApplication { public static void main(String[] args) { springApplication.run(SysApplication.class,args); }}
2)swagger处进行了配置
@Configuration@EnableSwagger2 //在swagger本身配置处进行了配置@ConditionalOnProperty(value = "swagger.manenabled", havingValue = "true")public class SwaggerConfig {}
实际在2)处的配置已经生效,但由于1)处有配置所以造成这个问题。
将1)处的配置关闭即可。
在Spring应用程序开发的过程中,可能需要根据配置属性的存在和值有条件地创建一些bean,@ConditionalOnProperty注解用于仅在环境属性存在且具有特定值时才启用 bean 注册。
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.METHOD})@Documented@Conditional({OnPropertyCondition.class})public @interface ConditionalOnProperty {//获取对应property名称的值,与name不可同时使用 String[] value() default {}; //配置属性名称的前缀 String prefix() default ""; //配置属性完整名称或部分名称//可与prefix组合使用,组成完整的配置属性名称,与value不可同时使用 String[] name() default {}; //可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置 String havingValue() default ""; //缺少该配置属性时是否可以加载。如果为true,没有该配置属性时也会正常加载;反之则不会生效 boolean matchIfMissing() default false;}
value以及 prefix + name 决定属性值(Property Value),以下为匹配规则
解析:假如havingValue为空,
属性值为boolean类型,则属性值为true 加载该bean,属性值为false 不加载该bean。
如果属性值不为boolean类型,则有该属性 则加载bean,没有该属性则不加载。
示例
@Bean(name = "emailNotification")@ConditionalOnProperty(prefix = "notification", name = "service")public NotificationSender notificationSender() { return new EmailNotification();}
如果配置该属性则加载:
notification.service=email
@Bean(name = "emailNotification")@ConditionalOnProperty(prefix = "notification", name = "enable")public NotificationSender notificationSender() { return new EmailNotification();}
如果配置为true则加载
notification.enable=true
如果缺失或为false则不加载
到此,关于“ConditionalOnProperty配置swagger不生效如何解决”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: ConditionalOnProperty配置swagger不生效如何解决
本文链接: https://www.lsjlt.com/news/340842.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0