Spring Boot ConfigurationProperties validate

24.7.4 @ConfigurationProperties Validation

Spring Boot will attempt to validate external configuration, by default using JSR-303 (if it is on the classpath).
You can simply add JSR-303 javax.validationconstraint annotations to your @ConfigurationProperties class:

@ConfigurationProperties(prefix="foo")
public class FooProperties {

    @NotNull
    private InetAddress remoteAddress;

    // ... getters and setters

}

In order to validate values of nested properties, you must annotate the associated field as @Valid to trigger its validation. For example, building upon the aboveFooProperties example:

@ConfigurationProperties(prefix="connection")
public class FooProperties {

    @NotNull
    private InetAddress remoteAddress;

    @Valid
    private final Security security = new Security();

    // ... getters and setters

    public static class Security {

        @NotEmpty
        public String username;

        // ... getters and setters

    }

}

You can also add a custom Spring Validator by creating a bean definition called configurationPropertiesValidator. The @Bean method should be declared static. The configuration properties validator is created very early in the application’s lifecycle and declaring the @Bean method as static allows the bean to be created without having to instantiate the @Configuration class. This avoids any problems that may be caused by early instantiation. There is a property validation sample so you can see how to set things up.

时间: 2024-08-26 08:49:53

Spring Boot ConfigurationProperties validate的相关文章

Spring Boot @ConfigurationProperties使用指导

1.简介 Spring Boot的一个非常有用的功能是外部化配置,并且可以轻松访问属性文件中定义的属性. 我们现在将详细地探索@ConfigurationProperties注释. 2.设置 本文使用相当标准的设置.我们首先在我们的pom.xml中添加spring-boot-starter-parent作为父项: <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframewor

Spring boot @ConfigurationProperties 和@Value

  @ConfigurationProperties @Value 功能 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法) 支持 不支持 SpEL 不支持 支持 JSR303数据校验 支持 不支持 复杂类型封装 支持 不支持 配置文件yml还是properties他们都能获取到值: 如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value: 如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperti

4、Spring Boot 自动配置原理

1.4 Spring Boot 自动配置原理 简介 spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate是不是在Classpath里面?如果是,并且DataSource也存在,就自动配置一个JdbcTemplate的Bean Thymeleaf是不是在Classpath里面?如果是,则自动配置Thymeleaf的模板解析器.视图解析器.模板引擎 那个这个是怎么实现的呢?原因就在于它利用了Spring的

在Spring Boot中使用 @ConfigurationProperties 注解

使用mail做例子.配置放在mail.properties文件中.属性必须命名规范才能绑定成功. Spring Boot 使用一些松的规则来绑定属性到@ConfigurationProperties bean 并且支持分层结构(hierarchical structure).开始创建一个@ConfigurationProperties bean: @ConfigurationProperties(locations = "classpath:mail.properties", igno

Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationPropertiesAutoConfiguration 4.总结 3.外部化配置的核心 ????????接着上一章,<Spring Boot 外部化配置(一)> 3.2 @ConfigurationProperties 众所周知,当 Spring Boot 集成外部组件后,就可在 propertie

玩转spring boot——properties配置

前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连接,若有一处配错或遗漏,就会带来不可挽回的损失.正因为这样,spring boot给出了非常理想的解决方案——application.properties.见application-properties的官方文档:http://docs.spring.io/spring-boot/docs/curr

翻译 Spring Boot How To

Spring Boot How To 1. 简介 本章节将回答一些常见的"我该怎么做"类型的问题,这些问题在我们使用Spring Boot时经常遇到.这绝不是一个详尽的列表,但它覆盖了很多方面. 如果遇到一个特殊的我们没有覆盖的问题,你可能想去查看stackoverflow.com 2. Spring Boot应用 2.1. 解决自动配置问题 Spring Boot自动配置总是尝试尽最大努力去做正确的事,但有时候会失败并且很难说出失败原因. 在每个Spring Boot Applica

六、spring boot 配置多数据源

spring boot 已经支持多数据源配置了,无需网上好多那些编写什么类的,特别麻烦,看看如下解决方案,官方的,放心! 1.首先定义数据源配置 #=====================multiple database config============================#ds1first.datasource.url=jdbc:mysql://localhost/test?characterEncoding=utf8&useSSL=truefirst.datasource

第5章 Spring Boot 功能

Spring Boot 功能 本节将会介绍Spring Boot的一些细节. 在这里,您可以了解您将要使用和自定义的主要功能. 如果还没有准备好,您可能需要阅读第二部分“入门指南”和第三部分“使用 Spring Boot”部分,以使您有基础的良好基础. 23. SpringApplication SpringApplication类提供了一种方便的方法来引导将从main()方法启动的Spring应用程序. 在许多情况下,您只需委派静态SpringApplication.run()方法: publ