2017-03-23 10 views
0

私は@ConfigurationPropertiesを使用して自分のプロパティを追跡しています。サブ構成用のクラスメンバーを含め、すべて正常に動作します。 Propertiesが正常に読み込まれ、フレームワークはゲッターを使用して「コンテキストの名前」を決定します。しかし、私は妥当性検査をしようとするときに問題がある、それはサブプロパティを検証しない。SpringプロパティネストされたConfigurationPropertiesでの検証

<?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); 
    } 

} 

メイン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 ... 
} 

サブConfigurationProperties、サブプロパティ.java

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

    ... getter & setter ... 
} 

だから、与えられたプロパティを使用して出力をファイル:私は値と値2で範囲外に行けば

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

を、すべてが(検証が動作します)細かいです:

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

Description: 

Binding to target [email protected] 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

あなたは@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 ... 
} 
+0

それだ:)。 –

関連する問題