@Configuration Properties:바인딩 클래스의 기본값
컨피규레이션클래스가 있어요FooConfig
거기서 나는 '푸'라는 제본 수업을 듣는다.
@Configuration
@ConfigurationProperties("foo")
public class FooConfig {
@Value("${foo.default.iterations}")
private Integer iterations;
private Foo foo;
// getter / setter
}
우리 반에서Foo
속성 파일에서 명시적으로 설정되지 않은 경우 기존 기본 구성 값을 사용하여 반복 변수를 설정합니다.
public class Foo {
private String name;
@Value("${foo.default.iterations}")
private Integer iterations;
// getter / setter
}
내 속성 파일
foo.default.iterations=999
# if this is set this config is bound (wins) in FooConfig-class as expected
# foo.iterations=111
foo.foo.name=foo
디폴트값 설정FooConfig
효과가 있지만, 내가 속한 클래스에서는 그렇지 않다.Foo
.
내가 뭘 놓쳤지?
섞으면 안 돼@Value
그리고.@ConfigurationProperties
같은 반에서디폴트값을 사용하는 경우@ConfigurationProperties
-syslog 클래스는 기본값으로 필드를 설정할 수 있습니다.
@ConfigurationProperties("foo")
public class FooConfig {
private Integer iterations = 999;
// getter / setter
}
이 변경에 의해, 다음과 같이 생성되는 메타데이터에 디폴트치를 포함할 수 있는 메리트가 추가됩니다.spring-boot-configuration-processor
메타데이터는 IDE에서 편집 시 자동 완료를 제공하기 위해 사용됩니다.application.properties
그리고.application.yaml
파일을 표시합니다.
마지막으로, 고객님의 문제와 직접 관련이 없는 경우@ConfigurationProperties
class에 주석을 붙이면 안 됩니다.@Configuration
. AN@Configuration
class는 class를 사용하여 bean을 설정합니다.@Bean
방법들.당신의.FooConfig
클래스에 주석을 달아야 합니다.@Component
그렇지 않으면@EnableConfigurationProperties(FooConfig.class)
에서@Configuration
사용하고 싶은 클래스FooConfig
.
디폴트값 입력resources/application.yml
그런 다음 외부 장치를 제공하여 재정의할 수 있습니다.application.yml
(패키지화된 항아리) 파일.
언급URL : https://stackoverflow.com/questions/51608371/configurationproperties-default-value-in-bound-class
'sourcetip' 카테고리의 다른 글
Javascript에서 Razor Model 개체에서 JSON 개체를 가져오는 방법 (0) | 2023.03.29 |
---|---|
익명 함수로 컨트롤러 래핑 (0) | 2023.03.29 |
Oracle의 여러 행에서 열 값을 연결하기 위한 SQL 쿼리 (0) | 2023.03.19 |
오류: 리스트가 맵 유형의 하위 유형이 아닙니다. (0) | 2023.03.19 |
Typescript가 클래스 및 인터페이스를 공개하기 위해 키워드 export를 사용하는 이유는 무엇입니까? (0) | 2023.03.19 |