2

は、この場合にはので、application.yml未解決のプレースホルダの検証春のブート設定のプロパティのために

my: 
    thing: ${missing-placeholder}/whatever 

私は@Value注釈を使用し、設定ファイル内のプレースホルダが検証されている次のように、解決不能のプレースホルダを持ついくつかのアプリケーション構成を考えます:

package com.test; 

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 

@Component 
public class PropValues { 
    @Value("${my.thing}") String thing; 
    public String getThing() { return thing; } 
} 

IllegalArgumentException: Could not resolve placeholder 'missing-placeholder' in value "${missing-placeholder}/whatever"となります。値がAbstractBeanFactory.resolveEmbeddedValueで直接設定されているためです。この場合、例えば、私はこの検証が欠落していることに気づい@ConfigurationPropertiesスタイルに移動するために探して、しかしPropertyPlaceholderHelper.parseStringValue

によってスローされた例外をキャッチするものは何もありません。

package com.test; 

import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.validation.annotation.Validated; 

@ConfigurationProperties(prefix = "my") 
public class Props { 
    private String thing; 
    public String getThing() { return thing; }  
    public void setThing(String thing) { this.thing = thing; } 
} 

例外はありません。私はのコメントで例外をキャッチし、無効な値を内部マップに集めることがわかります。PropertySourcesPropertyValues.getEnumerableProperty後続のデータバインディングでは、未解決のプレースホルダはチェックされません。

クラスとフィールドに@Validatedアノテーションと@Validアノテーションを適用するだけでは効果がないことが確認されました。

ConfigurationPropertiesバインドの未解決プレースホルダで例外をスローする動作を維持する方法はありますか?

+1

あなたがたが、 'フィールド上と検証のため番目のクラスと' @ NotNull'や '@ NotEmpty'上Validated' @は、あなたが持っている必要があります動作するようにすべきです'hibernate-validation'のようなクラスパス上のJSR-303バリデータです。アノテーション '@ Validation'を追加するだけでは何も得られません。 –

答えて

1

ちょうど10分前に同じ問題が発生しました。 お使いの設定でこのBeanを追加しよう:

@Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); 
     propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); 
     return propertySourcesPlaceholderConfigurer; 
    } 
+0

良い見つけましたが、実際には反対の問題の解決策です。私はエラーを隠したくない、私はそれを保存したい。 –

+0

これを行うには、プロパティのクラスを手動で作成して 'InitializingBean'を実装し、' afterPropertiesSet() 'メソッドをオーバーライドする必要があります。それぞれのフィールドの値を反映してチェックし、nullの場合に例外をスローすることができます。 – DeejonZ

+0

これは非常に優雅に聞こえません。プロパティはnullにならず、文字列に埋め込まれたプレースホルダの値になります。 –

関連する問題