详细介绍注解@ConfigurationProperties使用

写在前面:
我是「境里婆娑」。我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在心中信念丝毫未减,眼前这个少年,还是最初那张脸,面前再多艰险不退却。
写博客的目的就是分享给大家一起学习交流,如果您对 Java感兴趣,可以关注我,我们一起学习

前言:前两天在做项目时候,一个需求是要求两个bean启动要互斥,后来经过一番查找发现SpringBoot这个注解@ConfigurationProperties可以实现此功能。经过一番学习来分享给大家。

一、@ConfigurationProperties介绍

@ConditionalOnProperty一个最大的好处是可以控制@Configuration是否生效。
@ConditionalOnProperty属性介绍:

   String[] value() default {}; //数组,获取对应property名称的值,与name不可同时使用  
  
    String prefix() default "";//property名称的前缀,可有可无  
  
    String[] name() default {};//数组,property完整名称或部分名称(可与prefix组合使用,组成完整的property名称),与value不可同时使用  
  
    String havingValue() default "";//可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置  
  
    boolean matchIfMissing() default false;//缺少该property时是否可以加载。如果为true,没有该property也会正常加载;反之报错  
 

@ConditionalOnProperty如何控制@Configuration是否生效,具体操作是通过其两个属性name以及havingValue来实现的。

namehavingValue
truetrue
falsefalse
truefalse
falsetrue

name和havingValue属性相同的时候@Configuration生效,否则不生效。

一、@ConfigurationProperties使用详情

1、在application.properties添加配置文件

myapp.mail.localPath=zsl
myapp.mail.enable=true

2、创建两个配置类

Conditional1Configuration配置类

@Configuration
@ConfigurationProperties(prefix = "myapp.mail")
@ConditionalOnProperty(prefix = "myapp.mail",name = "enable",havingValue = "false")
public class Conditional1Configuration {

    private String localPath;

    private Boolean enable;

    @Bean
    public Conditional1 test1() {
       Conditional1 test = new Conditional1();
       test.setEnable(enable);
       test.setUser(localPath);
       return test;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }
}

ConditionalConfiguration配置类

@Configuration
@ConfigurationProperties(prefix = "myapp1.mail")
@ConditionalOnProperty(prefix = "myapp.mail",name = "enable",havingValue = "true",matchIfMissing = true)
public class ConditionalConfiguration {

    @Value("${myapp.mail.enable}")
    private Boolean enable;

    private String localPath;

    @Bean
    public Conditional test2() {
        Conditional test = new Conditional();
        test.setEnable(enable);
        test.setUser(localPath);
        return test;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }
}

当我把配置文件myapp.mail.enable改为true时候这个配置ConditionalConfiguration启动,当改为false时候 Conditional1Configuration启动。

至此,我们怎么使用注解@ConfigurationProperties介绍完毕。
如果想更详细查看以上所有代码请移步到github:如何使用注解@ConfigurationProperties

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

境里婆娑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值