Careeroza
Real World Example
ShopEase Configuration per Environment
A typical company project keeps one config per environment (profiles are explained in the Spring Boot material):
Profiles for dev and prod
# application.yml (shared)
spring:
jpa:
open-in-view: false
properties:
hibernate:
jdbc.time_zone: UTC
---
# application-dev.yml: developer laptop
spring:
datasource:
url: jdbc:mysql://localhost:3306/shopease_dev
username: root
password: root
jpa:
hibernate.ddl-auto: update
logging.level.org.hibernate.SQL: DEBUG
---
# application-prod.yml: production server
spring:
datasource:
url: jdbc:mysql://${DB_HOST}:3306/shopease
username: ${DB_USER}
password: ${DB_PASSWORD}
hikari:
maximum-pool-size: 20
jpa:
hibernate.ddl-auto: validate # never change prod schema automaticallyExample output
$ java -jar shopease.jar --spring.profiles.active=prod INFO ShopEaseApplication : The following 1 profile is active: "prod" INFO HikariDataSource : HikariPool-1 - Start completed. INFO LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Why These Choices
- Passwords from environment variables: never commit production credentials to Git.
ddl-auto: validatein prod: the app refuses to start if an entity and the table disagree, instead of corrupting data.open-in-view: false: avoids hidden database queries while rendering the response (explained in the advance level).jdbc.time_zone: UTC: all timestamps stored in UTC; convert to IST only for display.- SQL logging only in dev.
Exercise
Exercise: Write an application.yml for a test profile that uses an in-memory H2 database, recreates the schema on each run and logs SQL with parameter values.
Show answer
spring:
datasource:
url: jdbc:h2:mem:shopease_test
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
format_sql: true
logging:
level:
org.hibernate.SQL: DEBUG
org.hibernate.orm.jdbc.bind: TRACE