Spring Boot에서 애플리케이션 속성 액세스
Spring Boot 애플리케이션 내의 application.properties 파일에 정의된 값을 검색해야 합니까? 방법은 다음과 같습니다.
@Value Annotation
@Value 주석을 사용하면 Spring Bean에 속성 값을 주입할 수 있습니다. 예를 들어 userBucket.path에 액세스하려면:
@Value("${userBucket.path}") private String userBucketPath;
외부 구성
Spring Boot는 다양한 소스의 속성 값에 액세스할 수 있는 포괄적인 외부 구성 메커니즘을 제공합니다. , 포함 application.properties.
@ConfigurationProperties
@ConfigurationProperties 주석을 사용하여 Bean을 속성 소스에 매핑합니다. 이를 통해 빈 내의 필드에 속성 값을 직접 바인딩할 수 있습니다. 예:
@ConfigurationProperties(prefix = "userBucket") public class BucketProperties { private String path; // ... getters and setters }
@PropertySource
@PropertySource를 사용하여 사용자 정의 소스에서 속성 로드:
@PropertySource("classpath:my-custom-properties.properties") public class MyProperties { @Value("${my-custom-property}") private String customProperty; }
@Environment
@Environment 인터페이스는 다음을 제공합니다. 현재 환경 및 해당 속성에 대한 액세스:
Environment env = SpringApplication.getEnvironment(); String customProperty = env.getProperty("my-custom-property");
자세한 내용과 구성 옵션은 외부 구성에 대한 Spring Boot 설명서(https://docs.spring.io/spring-boot/docs)를 참조하세요. /current/reference/html/boot-features-external-config.html
위 내용은 Spring Boot 애플리케이션에서 애플리케이션 속성에 어떻게 액세스할 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!