は、この場合にはので、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
バインドの未解決プレースホルダで例外をスローする動作を維持する方法はありますか?
あなたがたが、 'フィールド上と検証のため番目のクラスと' @ NotNull'や '@ NotEmpty'上Validated' @は、あなたが持っている必要があります動作するようにすべきです'hibernate-validation'のようなクラスパス上のJSR-303バリデータです。アノテーション '@ Validation'を追加するだけでは何も得られません。 –