BookStore Spring - .env file setup / .env configuration with application.yml

Byung Joo JeongByung Joo Jeong
2 min read

Foreword

After I created Spring Boot Project, and linked it to remote repositories.

For DB connection and other configuration (will add JWT, cookie, Email and etc), I want to enclose private information.

Using .env file can be useful for it.

( .env + application.yml pair )

Consequently,

.env -> private info, add it to .gitignore

application.yml -> add place holder that alines with .env file -> will be updated to gitHub

1.Add dependencies for reading .env file

Add dependencies

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.2'
    id 'io.spring.dependency-management' version '1.1.6'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
       languageVersion = JavaLanguageVersion.of(17)
    }
}

configurations {
    compileOnly {
       extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

    //For reading .env file (Both dependencies below work)
    implementation 'io.github.cdimascio:java-dotenv:+'
    //implementation 'io.github.cdimascio:java-dotenv:5.2.2'
}

tasks.named('test') {
    useJUnitPlatform()
}

2.Create .env file

: hover on BookStore directory, and then create File .env

3.Add configuration in .env

BookStore (dir) -> .env


DB_URL=jdbc:mysql://localhost:3306/BookStore
DB_USERNAME=root
DB_PASSWORD=password1234

4.Add placeholders for .env file to application.yml

src -> resources -> application.yml

spring:
  datasource:
    url: ${DB_URL}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQLDialect
    hibernate:
      ddl-auto: create
      show-sql: true

5.Add .env to .gitignore

6.Run applicationon

runs well!

Consequently

.env -> private info, add it to .gitignore

application.yml -> add place holder that alines with .env file -> will be updated to gitHub


Error from misconfiguration

:: this is for logging, documentation above works well. you can follow its reference.

1.application.yml misconfiguration

src -> resources -> application.yml

spring:
  datasource:
    url: ${DB_URL}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: create

\==> Error (URL must start with 'jdbc')

Resolve


2.build.gradle for .env - wrong dependency

Both dependencies above work.

0
Subscribe to my newsletter

Read articles from Byung Joo Jeong directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Byung Joo Jeong
Byung Joo Jeong