次のように、解決できないプレースホルダーを含むアプリケーション構成が与えられた場合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
おり、によってスローされた例外をキャッチするものが何もないためです。PropertyPlaceholderHelper.parseStringValue
ただし、スタイルに移行しようとすると@ConfigurationProperties
、この検証が欠落していることに気付きました。たとえば、次の場合です。
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
コメントで例外をキャッチし// Probably could not resolve placeholders, ignore it here
、無効な値を内部マップに収集することがわかります。以降のデータ バインディングでは、未解決のプレースホルダーはチェックされません。
@Validated
クラスとフィールドにとアノテーションを適用するだけで@Valid
は役に立たないことを確認しました。
ConfigurationProperties
バインドされた未解決のプレースホルダーで例外をスローする動作を保持する方法はありますか?