在Spring Boot中使用 @ConfigurationProperties 注解

 

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

上一篇博客写了 如何在Spring Boot application中配置mail . 使用 @Value 注解注入属性. 但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean。 这里会介绍如何使用@ConfigurationProperties。 继续使用mail做例子。配置放在mail.properties文件中。属性必须命名规范才能绑定成功。举例: 1 protocol and PROTOCOL will be bind to protocol field of a bean 2 smtp-auth , smtp_auth , smtpAuth will be bind to smtpAuth field of a bean 3 smtp.auth will be bind to … hmm to smtp.auth field of a bean!

Spring Boot 使用一些松的规则来绑定属性到@ConfigurationProperties bean 并且支持分层结构(hierarchical structure)。 开始创建一个@ConfigurationProperties bean:


@ConfigurationProperties(locations = "classpath:mail.properties", 
                         ignoreUnknownFields = false, 
                         prefix = "mail") // 前辍为mail 
public class MailProperties { 
  public static class Smtp {  
    private boolean auth;  
    private boolean starttlsEnable;  
    // ... getters and setters 
  }
  @NotBlank private String host;
  private int port;  
  private String from; 
  private String username;
  private String password; 
  @NotNull private Smtp smtp; 
  // ... getters and setters
}

…从如下属性中创建 ( mail.properties ):


mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me@localhost
mail.username=
mail.password=

上例中我们用@ConfigurationProperties注解就可以绑定属性了。ignoreUnknownFields = false告诉Spring Boot在有属性不能匹配到声明的域的时候抛出异常。开发的时候很方便! prefix 用来选择哪个属性的prefix名字来绑定。
请注意setters 和 getters 需要在@ConfigurationProperties bean中创建! 与@Value注解相反, 这带来了代码中的一些困扰 (特别是简单的业务中,个人观点).
OK,但是我们需要用属性来配置 application. 有至少两种方式来创建@ConfigurationProperties。即可以搭配@Configuration 注解来提供 @Beans 也可以单独使用并注入 @Configuration bean。

方案1:


@Configuration
@ConfigurationProperties(locations = "classpath:mail.properties", 
                         prefix = "mail")
public class MailConfiguration { 
  public static class Smtp {
    private boolean auth;
    private boolean starttlsEnable;
    // ... getters and setters
 }

 @NotBlank private String host; 
 private int port;
 private String from; 
 private String username;
 private String password; 

 @NotNull private Smtp smtp; 
 // ... getters and setters  
 
 @Bean public JavaMailSender javaMailSender() {
  // omitted for readability
 }
}

方案2

我们和上面例子一样注解属性,然后用 Spring的@Autowire来注入 mail configuration bean:


@Configuration
@EnableConfigurationProperties(MailProperties.class)
 public class MailConfiguration { 
    @Autowired private MailProperties mailProperties; // mailProperties.getxxx得到配置中的值。

    @Bean public JavaMailSender javaMailSender() {
      // omitted for readability
    }
 }

请注意@EnableConfigurationProperties注解。 这个注解告诉Spring Boot 使能支持@ConfigurationProperties。如果不指定会看到如下异常:


org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [demo.mail.MailProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

注意: 还有其他办法 (Spring Boot 总是有其他办法!) 让@ConfigurationProperties beans 被添加 – 用@Configuration或者 @Component注解, 这样就可以在 component scan时候被发现了。

总结:

@ConfigurationProperties很方便使用。 比用@Value注解好吗? 在特定的方案中是的,这只是一个选择问题。
看下Spring Boot的文档有更多的关于 typesafe configuration 属性

 


作者:crocodile_b
链接:https://www.jianshu.com/p/df57fefe0ab7
來源:简书
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值