[Spring] @Value static 변수 연결

Taeho·2022년 9월 13일
0

SpringFramework

목록 보기
4/4

스프링프레임웍에서 static 변수에 값을 바인딩 할 수 없다.

다음과 같은 코드에서 batchdomain 변수의 값을 null 이다.

    @Value("${common.batch.domain}")
    private static String batchdomain;

non static 메소드를 통해 받은 변수를 static 변수에 넣을 수 있다.

@Component
public class BatchUtil {

    private static String batchdomain;

    @Value("${common.batch.domain}")
    public void setBatchdomain(String batchdomain) {
        BatchUtil.batchdomain = batchdomain;
    }

    public static void registerToBatchServer() {
    ...

    	HashMap<String, Object> result = new HttpUtil()
                .url(BatchUtil.batchdomain + "/api/v1/batch/definition/" + batchName)
                .method("get")
                .build();
	...
    }

참고자료

properties와 value 사용시 static 변수의 연결

Injecting a Value in a Static Field in Spring

Spring - 환경변수 static 변수에 넣기

profile
한걸음 더 내딛는 개발자

0개의 댓글