@Value 로 application.yml 의 값을 가져오는데 해당 에러가 발생했다.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationProperty': Injection of autowired dependencies failed;
nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder '' in value "${}"
BeanCreationException
말 그대로 스프링빈 객체 생성중에 특정 예외가 발생하여 빈 생성이 되지 못할 때 발생하는 예외이다.
특정 필드에 의존주입이 되어 있을것으로 예상했지만 의존주입에 실패했기 때문에 발생하는 것이다.
Application.yml
application:
~~~~:
~~~~~:
-
@Value 값
@Value("${application.~~~~~}")
private List<String> name;
대부분의 원인은
@Value("${application.~~~~}")를 통해 application.~~~~ 이라는 properties 값을 주입받는다고 해놓고,
application.yml 에는 해당 값을 입력하지 않아 발생하는 경우가 많다.
혹은 철자, 기호, 공백 등 으로 인한 오류인 경우가 많다.
하지만 나는 토씨 하나 안 틀렸는데?
두 눈 씻고 봐도 맞게 했다. 왜 안 될까?
원인은 어이없고 황당한 이유였다.
Appliacation.yml 에서는 - 를 list로 인식한다.
위에 보이는 application.~~~~ 는 List 이다.
나는 yml에 해당 값을 비워뒀는데, 값이 없는 List를 계속 주입시키려고 해서 Exception이 발생한 것이었다.
누굴 탓하냐.. 값 안 넣고 부른 내 탓이지...
해결 방법
@Value("${application.~~~~~:@null}")
private List<String> name;
:(콜론) 뒤에 기본 값 지정 시 참조하고 있는 .properties 파일에 값이 입력되지 않았을 때 기본 값을 설정할 수 있다.
null 과 @null 의 차이점은 문자열과 null 의 차이이다.
null 은 "null" 문자열이 주입되며, @null 은 null값이 들어간다.
+
해당 방법은 임시이고, 실 사용 시 null 값일 경우 에러가 날 수 있다.
경로를 전부 맞게 했는데도 계속 에러가 발생할 경우 해결법
property 값을 별도 class 파일로 나눠 새로 생성해준다.
@Configuration
@EnableConfigurationProperties
public class SampleProperty {
@Component
@ConfigurationProperties(prefix = "application.~~~")
@Data
public class ApplicationSample1 {
private String[] parttern;
}
@Component
@ConfigurationProperties(prefix = "application.~~")
@Data
public class ApplicationSample2 {
private String dddkey;
}
}
나 같은 사람이 또 있을까봐 포스팅 했다.
도움이 되었기를 바라며.
https://devel-log.tistory.com/63