Spring MVC - @PropertySource 사용 중 FileNotFoundException: Could not open ServletContext resource 에러

2021. 4. 22. 03:11 Spring Framework/Spring MVC

Spring MVC - @PropertySource 사용 중 FileNotFoundException: Could not open ServletContext resource 에러

⚙️ Spring Web MVC 5.2.6.RELEASE

 

스프링 웹 MVC 프로젝트에서 프로퍼티를 사용하다가 다음과 같은 에러가 발생하였다.

java.io.FileNotFoundException: Could not open ServletContext resource [/properties/data1.properties]

 

프로젝트의 프로퍼티 파일 위치는 src/main/resources/properties/data1.properties

 

메이븐 프로젝트이기 때문에 빌드하면 resources 내의 파일들이 classpath로 복사된다.

파일을 왜 못찾지.. 하면서 target 디렉토리를 열어봤지만 문제 없이 복사되었다.

 

그리고 @PropertySource로 프로퍼티 파일을 설정한 코드는:

@PropertySource("/properties/data1.properties")

 

이제 무엇이 잘못됐는지 많이들 알 것이다.

classpath: prefix를 적지 않았다.

 

classpath 내의 프로퍼티 파일을 지정할때는 classpath: prefix를 꼭 붙이자. 😅👇

@PropertySource("classpath:/properties/data1.properties")

 

아주 흔한 실수다.. 🙈

 

혹시 스프링 프로젝트에서 @PropertySource로 지정한 프로퍼티 파일을 찾지 못해 FileNotFoundException이 난다면 같은 문제가 아닌지 확인해 보자!

 

출처 : atoz-develop.tistory.com/entry/Spring-MVC-PropertySource-%EC%82%AC%EC%9A%A9-%EC%A4%91-FileNotFoundException-Could-not-open-ServletContext-resource-%EC%97%90%EB%9F%AC?category=869241