๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring MVC - @PropertySource ์‚ฌ์šฉ ์ค‘ FileNotFoundException: Could not open ServletContext resource ์—๋Ÿฌ

by Leica 2020. 5. 6.
๋ฐ˜์‘ํ˜•

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์ด ๋‚œ๋‹ค๋ฉด ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด ๋ณด์ž!

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€