Spring
Spring - static 변수에 @Value 어노테이션 적용
codeManager
2022. 9. 5. 18:39
반응형
Spring boot에서 application.yml에 저장된 설정값을 가져올 때 @Value Annotation을 사용합니다.
보통은 이런 식으로 @Configuration에서 설정값을 매핑시킵니다.
@Configuration
public class AWSConfig {
@Value("${cloud.aws.region.static}")
private String region;
}
그런데 @Component에서 @Value 어노테이션을 설정값을 가져오면 에러가 발생합니다.
@Component
public class S3Adapter {
@Value("${cloud.aws.region.static}")
private String region;
}
에러 메시지
Description:
Parameter 0 of constructor in com.example.S3Adapter required a bean of type 'java.lang.String' that could not be found.
Action: Consider defining a bean of type 'java.lang.String' in your configuration.
해결 방법은 setter 메소드를 추가해서 변수에 직접 값을 넣을 수 있도록 하면 됩니다.
@Component
public class S3Adapter {
private static String region;
@Value("${cloud.aws.region.static}")
public void setRegion(String value) {
region = value;
}
}
반응형