개발
spring.config.import 설정으로 env 파일을 읽을 때 생길 수 있는 오류
모달조아
2023. 4. 22. 05:35
spring:
config:
import:
- optional:file:env/dev-db.env
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
jpa:
database: mysql
database-platform: org.hibernate.dialect.MySQL8Dialect
open-in-view: false
properties:
hibernate:
show_sql: true
format_sql: true
위와 같이 env 파일을 읽어와서 사용하려고 하였는데 아래와 같은 에러가 발생했다.
Caused by: java.lang.IllegalStateException: File extension is not known to any PropertySourceLoader. If the location is meant to reference a directory, it must end in '/' or File.separator
PropertySourceLoader 가 읽을 수 없는 확장자라는 의미이다.
기본적으로 PropertySourceLoader 가 제공하는 확장자는 .properties, .yml, .json, .xml 이다.
사실 생각해보면 당연하다. .env 라는 확장자는 관례상 환경변수를 나타내는 확장자이지만 공식적인 파일 형식은 아니다. 그러므로 아래와 같이 뒤에 확장자를 명시해주면 에러를 해결할 수 있다.
spring:
config:
import:
- optional:file:env/dev-db.env[.properties]
env 파일을 properties 형식으로 작성해줬으므로 [.properties] 를 붙혀줬다.