配置Spring Boot通过@ConditionalOnProperty来控制Configuration是否生效

配置Spring Boot通过@ConditionalOnProperty来控制Configuration是否生效

qny: AccessKey: 123 SecretKey: 123 bucket: aaa enable:true

一般动态配置我们会用@value注解或者实现InitializingBean,这里我用的是七牛云的举例,为了解耦合动态的去管理配置文件生效

@Data 
@ConfigurationProperties(prefix=”qny”)
 public class QnyProperties { 
 private String AccessKey; 
 private String SecretKey;
 private String bucket; 
  } 
 

我这里用@ConfigurationProperties(prefix=”qny”) 读取配置文件以前缀为qny去注入,但是还没有交由spring 进行管理配置没有生效
在这里用@EnableConfigurationProperties交由Spring容器通过配置文件去创建QnyProperties

@RequiredArgsConstructor
@EnableConfigurationProperties({QnyProperties.class})
@org.springframework.context.annotation.Configuration
public class QnyAutoConfiguration {
    public final QnyProperties qnyProperties;

    @Bean
    @ConditionalOnMissingBean(QnyProperties.class)
    @ConditionalOnProperty(name = "qny.enable", havingValue = "true", matchIfMissing = true)
    public Configuration configuration(){
        return new Configuration(Region.region0());
    }

    @Bean
    @ConditionalOnProperty(name = "qny.enable", havingValue = "true", matchIfMissing = true)
    public UploadManager uploadManager(Configuration configuration){
        return new UploadManager(configuration);
    }

    @Bean
    @ConditionalOnMissingBean(QnyProperties.class)
    @ConditionalOnProperty(name = "qny.enable", havingValue = "true")
    public String upToken(){
        Auth auth = Auth.create(qnyProperties.getAccessKey(), qnyProperties.getSecretKey());
        return auth.uploadToken(qnyProperties.getBucket());
    }



}

这里的核心注解是Spring boot中有个注解@ConditionalOnProperty,这个注解能够控制某个configuration是否生效。具体操作是通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot的@ConfigurationProperties注解用于将配置文件中的属性与Java类中的属性进行绑定。如果在使用Spring Boot 2.4.5版本时@ConfigurationProperties注解不生效,可能是由于以下几个原因: 1. 未添加@Configuration注解:在使用@ConfigurationProperties注解的类上要添加@Configuration注解,以确保该类被Spring容器扫描并创建Bean。 2. 未在Spring Boot的入口类上添加@EnableConfigurationProperties注解:如果@ConfigurationProperties注解的类不是在Spring Boot的入口类中被创建的Bean,需要在入口类上添加@EnableConfigurationProperties注解来启用@ConfigurationProperties注解的生效。 3. 配置文件中的前缀不正确:@ConfigurationProperties注解中的prefix属性用于指定配置文件中的属性前缀,要确保该前缀与配置文件中的属性前缀一致。 4. 未在配置文件中添加对应的属性:如果使用@ConfigurationProperties注解将配置文件中的属性与Java类中的属性进行绑定,要确保配置文件中存在对应的属性。 5. 未使用setter方法:在使用@ConfigurationProperties注解绑定属性时,需要提供相应的setter方法,以便Spring Boot能够将属性值设置到Java类中的属性中。 如果以上原因都排除了还是无法生效,可以尝试重新编译并清理项目,或者考虑升级到最新版本的Spring Boot。另外,也可以在Spring Boot应用启动时打印日志,查看@ConfigurationProperties相关的日志输出,以便进一步排查问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值