728x90
반응형
1. 파일설정
application.yml
# default = local
spring:
profiles:
default: local
datasource:
url: ${LOCAL_DB_URL}
username: ${LOCAL_DB_USERNAME}
password: ${LOCAL_DB_PASSWORD}
jpa:
database: MYSQL
show-sql: true
properties:
hibernate.format_sql: true
hibernate.use_sql_comments: true
hibernate.highlight_sql: true
hibernate:
ddl-auto: validate
security:
oauth2:
client:
registration:
google:
client-id: ${LOCAL_OAUTH_GOOGLE_CLIENT_ID}
client-secret: ${LOCAL_OAUTH_GOOGLE_CLIENT_SECRET}
.env 파일
2. 구성 및 적용
build.gradle.kts에 dotenv impl
implementation("io.github.cdimascio:dotenv-kotlin:6.4.0")
Main.kt 파일
Dotenv.load()로 가져온 key-value 값들을 DotenvEntry class로 치환하고, 그 값들을 SpringApplicationBuilder의 .environment에 customizePropertySource 메서드를 override해서 넣겠다는 방식으로 이해됨
import io.github.cdimascio.dotenv.Dotenv
import io.github.cdimascio.dotenv.DotenvEntry
fun main(args: Array<String>) {
// Load environment variables from the .env file
val env: Map<String, Any> = Dotenv.load()
.entries()
.stream()
.collect(
Collectors.toMap(DotenvEntry::getKey, DotenvEntry::getValue)
)
SpringApplicationBuilder(Main::class.java)
.environment(object : StandardEnvironment() {
override fun customizePropertySources(propertySources: MutablePropertySources) {
super.customizePropertySources(propertySources)
propertySources.addLast(MapPropertySource("dotenvProperties", env))
}
}).run(*args)
}
3. 주의사항
.gitigore에 .env 추가!
intellij라면 .env 파일이 노란색으로 바뀌는지 확인! 잘 안되면 IDE 재시작하거나, commit 전에 .env 제거하고 git add
참고
728x90
반응형