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 직접 설정하고 그러나 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이 예외를 포착하고 내부 맵에 유효하지 않은 값을 수집하는 것을 볼 수 있습니다. 후속 데이터 바인딩은 확인되지 않은 자리 표시자를 확인하지 않습니다.

클래스 및 입력란에 @Validated@Valid 특수 효과를 적용하는 것만으로는 도움이되지 않습니다.

ConfigurationProperties 바인딩을 사용하여 해결되지 않은 자리 표시 자에 대한 예외를 throw하는 동작을 유지할 수있는 방법이 있습니까?

+1

당신은 그러나'필드에서와 검증을위한 일 클래스와'@ NotNull' 또는'@ NotEmpty'에 Validated' @ 당신이 있어야 할 것이다 일을한다 'hibernate-validation'과 같은 클래스 경로의 JSR-303 유효성 검사기. 어노테이션'@ Validation '만 추가하면 아무것도 산출되지 않습니다. –

답변

1

정확히 10 분 전에 같은 문제가있었습니다. 구성이 콩을 추가하십시오 :

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

좋은 찾기는 사실이지만 그 반대의 문제에 대한 해결책입니다. 나는 오류를 숨기고 싶지 않아, 나는 그것을 보존하고 싶다. –

+0

당신은 수동으로 속성 클래스를 만들어'InitializingBean'을 구현하고'afterPropertiesSet()'메소드를 오버라이드해야합니다. 각 필드의 값을 리플렉션하여 검사하고 null 인 경우에 예외를 throw 할 수 있습니다. – DeejonZ

+0

그것은 매우 우아하게 들리지 않습니다. 속성이 null이되지 않는 것 외에도 문자열에 여전히 포함 된 자리 표시 자 값으로 끝납니다. –