• Spring Boot @ConfigurationProperties使用指导


    1.简介

    Spring Boot的一个非常有用的功能是外部化配置,并且可以轻松访问属性文件中定义的属性

    我们现在将详细地探索@ConfigurationProperties注释。

    2.设置

    本文使用相当标准的设置。我们首先在我们的pom.xml中添加spring-boot-starter-parent作为父项

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
     

    为了能够验证文件中定义的属性,我们还需要JSR-303的实现。hibernate-validator就是其中之一。我们也将它添加到我们的pom.xml中:

    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-validator</artifactId>
       <version>6.0.16.Final</version>
    </dependency>

    3.简单属性

    官方文档建议我们将配置属性隔离到单独的POJO中那么让我们从这开始:

     1 @Configuration
     2 @PropertySource("classpath:configprops.properties")
     3 @ConfigurationProperties(prefix = "mail")
     4 public class ConfigProperties {
     5      
     6     private String hostName;
     7     private int port;
     8     private String from;
     9  
    10     // standard getters and setters
    11 }
     

    我们使用@Configuration,以便Spring在应用程序上下文中创建一个Spring bean。

    我们还使用@PropertySource来定义属性文件的位置。否则,Spring使用默认位置(classpath:application.properties)。

    @ConfigurationProperties最适用于具有相同前缀的分层属性。所以我们这里添加一个前缀mail

    Spring框架使用标准的Java bean设置器,因此我们需要为每个属性声明setter方法(用于为字段赋值)。

    注意:如果我们不在POJO中使用@Configuration ,那么我们需要在主Spring应用程序类中添加@EnableConfigurationProperties(ConfigProperties.class) 以将属性绑定到POJO:

    @SpringBootApplication
    @EnableConfigurationProperties(ConfigProperties.class)
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
     

    完事!Spring将自动绑定在我们的属性文件中定义的任何属性,该属性具有前缀mail并且后续字段ConfigProperties中的一个字段有相同的名称

    Spring使用一些宽松的绑定属性规则。因此以下变体都绑定到属性hostName

    mail.hostName
    mail.hostname
    mail.host_name
    mail.host-name
    mail.HOST_NAME
     

    我们可以使用以下属性文件来设置所有字段:

    #Simple properties
    mail.hostname=host@mail.com
    mail.port=9000
    mail.from=mailer@mail.com
     

    4.嵌套属性

    我们可以在list、map类中使用嵌套属性

    让我们创建一个新的Credentials类,用于一些嵌套属性:

    public class Credentials {
        private String authMethod;
        private String username;
        private String password;
     
        // standard getters and setters
    }
     

    我们还更新ConfigProperties类使用List,一个MapCredentials 类:

    public class ConfigProperties {
     
        private String host;
        private int port;
        private String from;
        private List<String> defaultRecipients;
        private Map<String, String> additionalHeaders;
        private Credentials credentials;
      
        // standard getters and setters
    }
     

    以下属性文件将设置所有字段:

    #Simple properties
    mail.hostname=mailer@mail.com
    mail.port=9000
    mail.from=mailer@mail.com
     
    #List properties
    mail.defaultRecipients[0]=admin@mail.com
    mail.defaultRecipients[1]=owner@mail.com
     
    #Map Properties
    mail.additionalHeaders.redelivery=true
    mail.additionalHeaders.secure=true
     
    #Object properties
    mail.credentials.username=john
    mail.credentials.password=password
    mail.credentials.authMethod=SHA1
     

    5.属性验证

    @ConfigurationProperties使用JSR-303格式提供属性验证。这允许各种整洁的东西。

    例如,让我们强制使用hostName属性:

    @NotBlank
    private String hostName;
     

    并且authMethod属性长度为1到4个字符:

    @Length(max = 4, min = 1)
    private String authMethod;
     

    端口属性从1025到65536:

    @Min(1025)
    @Max(65536)
    private int port;
     

    最后,from属性必须与电子邮件地址格式匹配:

    @Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$")
    private String from;
     

    这有助于我们减少代码中的大量if - else条件,使其看起来更清晰简洁。

    如果这些验证中的任何一个失败,那么主应用程序将无法启动,报错信息IllegalStateException

    Hibernate Validation框架使用标准的Java bean getter和setter,因此我们为每个属性声明getter和setter非常重要。

    六,结论

    我们已经探索了@ConfigurationProperties注释,并看到了它提供的一些方便的功能,如轻松绑定和Bean验证。

  • 相关阅读:
    apache伪静态设置
    ZeroClipboard.js兼容各种浏览器复制到剪切板上
    table 如何给tr border颜色
    JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
    jquery操作select(增加,删除,清空)
    JS生成随机的由字母数字组合的字符串
    Redis连接(二)
    Redis集群(一)
    wap启用宏
    windows 10激活
  • 原文地址:https://www.cnblogs.com/gc65/p/10618395.html
Copyright © 2020-2023  润新知