Spring嵌套ConfigurationProperties

时间:2017-03-23 13:01:27

标签: java spring validation

我正在使用@ConfigurationProperties来跟踪我的属性。一切正常,包括子配置的类成员。 Properties加载良好,框架使用getter来确定“上下文的名称”。但是,我在尝试验证时遇到问题,它不验证子属性。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>be.test</groupId>
   <artifactId>test</artifactId>
   <version>1.0-SNAPSHOT</version>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.2.RELEASE</version>
   </parent>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <dependency>
         <groupId>javax.validation</groupId>
         <artifactId>validation-api</artifactId>
         <version>1.1.0.Final</version>
      </dependency>
      <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-validator</artifactId>
         <version>5.4.1.Final</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

application.properties:

test.value = 4
test.value2 = 6
test.sub.subValue = 9

ValidationApplication.java

@SpringBootApplication
public class ValidationApplication implements CommandLineRunner
{

   private final TestProperties properties;

   public ValidationApplication(TestProperties properties) {
      this.properties = properties;
   }

   @Override
   public void run(String... args) {
      System.out.println("=========================================");
      System.out.println("Value: " + this.properties.getValue());
      System.out.println("Value2: " + this.properties.getValue2());
      System.out.println("SubValue: " + this.properties.getSub().getSubValue());
      System.out.println("=========================================");
   }

   public static void main(String[] args) throws Exception {
      new SpringApplicationBuilder(ValidationApplication.class).run(args);
   }

}

Main ConfigurationProperties,TestProperties.java

@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties
{
   @Min(4)
   private int value;

   @Max(6)
   private int value2;

   private SubProperties sub;

   ... getters and setters ...
}

Sub ConfigurationProperties,SubProperties.java

@Component
@ConfigurationProperties
public class SubProperties
{
   @Max(10)
   private int subValue;

   ... getter & setter ...
}

所以具有给定属性文件的输出:

=========================================
Value: 4
Value2: 6
SubValue: 9
=========================================

如果我使用Value和Value2超出范围,一切都很好(验证工作):

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target be.test.TestProperties@6cb6decd failed:

    Property: test.value2
    Value: 7
    Reason: must be less than or equal to 6

    Property: test.value
    Value: 2
    Reason: must be greater than or equal to 4


Action:

Update your application's configuration

然而,使用这些值:

test.value = 4
test.value2 = 6
test.sub.subValue = 11

验证不会触发(即使子属性存在@Max约束。)

这是正常的吗?

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您错过了@Valid注释:

@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties
{
   @Min(4)
   private int value;

   @Max(6)
   private int value2;

   @Valid
   private SubProperties sub;

   ... getters and setters ...
}