@Configuration Properties를 @Configuration으로 자동 배선하는 방법은 무엇입니까?
다음과 같이 정의된 속성 클래스가 있습니다.
@Validated
@ConfigurationProperties(prefix = "plugin.httpclient")
public class HttpClientProperties {
...
}
다음과 같은 구성 클래스:
@Configuration
@EnableScheduling
public class HttpClientConfiguration {
private final HttpClientProperties httpClientProperties;
@Autowired
public HttpClientConfiguration(HttpClientProperties httpClientProperties) {
this.httpClientProperties = httpClientProperties;
}
...
}
스프링 부트 애플리케이션을 시작할 때, 나는
Parameter 0 of constructor in x.y.z.config.HttpClientConfiguration required a bean of type 'x.y.z.config.HttpClientProperties' that could not be found.
유효한 사용 사례가 아닌가요, 아니면 종속성을 어떻게든 선언해야 하나요?
이것은 유효한 사용 사례이지만,HttpClientProperties
구성 요소 스캐너에서 스캔되지 않았기 때문에 픽업되지 않습니다.주석을 달 수 있습니다.HttpClientProperties
와 함께@Component
:
@Validated
@Component
@ConfigurationProperties(prefix = "plugin.httpclient")
public class HttpClientProperties {
// ...
}
이를 위한 또 다른 방법은 (스테판 니콜이 언급한 바와 같이)@EnableConfigurationProperties()
Spring 구성 클래스에 대한 주석, 예:
@EnableConfigurationProperties(HttpClientProperties.class) // This is the recommended way
@EnableScheduling
public class HttpClientConfiguration {
// ...
}
이는 Spring 부트 문서에도 설명되어 있습니다.
Spring Boot 2.2.1+에서 다음을 추가합니다.@ConfigurationPropertiesScan
응용 프로그램에 대한 주석.(버전 2.2.0에서는 기본적으로 이 기능이 활성화되어 있습니다.) 이렇게 하면 모든 클래스에 주석을 달 수 있습니다.@ConfigurationProperties
사용하지 않고 픽업되는@EnableConfigurationProperties
또는@Component
.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
@SpringBootApplication
@ConfigurationPropertiesScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
또한 주석이 달린 클래스에 대한 메타데이터를 생성합니다.@ConfigurationProperties
IDE가 application.properties에서 자동 완성 및 문서화를 제공하는 데 사용하는 이 도구는 다음과 같은 종속성을 추가해야 합니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
언급URL : https://stackoverflow.com/questions/43797924/how-to-autowire-configurationproperties-into-configuration
'programing' 카테고리의 다른 글
Spring 프레임워크를 구성하는 방법에는 몇 가지가 있습니까?기술적으로 그들 사이의 차이점은 무엇입니까? (찬성도 반대도 아닙니다.) (0) | 2023.07.24 |
---|---|
루프용 Javascript 내부의 비동기 프로세스 (0) | 2023.07.24 |
node.js, mysql, 날짜 및 시간 오프셋 (0) | 2023.07.24 |
Powershell: 존재하지 않을 수 있는 경로를 확인하시겠습니까? (0) | 2023.07.24 |
Node.js - 현재 파일 이름 가져오기 (0) | 2023.07.24 |