Careeroza
Profiles dev test and prod
One App, Many Environments
Your laptop uses H2 and fake payments; production uses MySQL and Razorpay. A profile is a named set of configuration. Spring loads application.yml first, then application-{profile}.yml on top of it (profile values win).
src/main/resources/
├── application.yml # shared defaults
├── application-dev.yml # laptop
├── application-test.yml # automated tests
└── application-prod.yml # productionProfile Files
Example
application.yml
server:
port: 8080
spring:
application:
name: shopease
jpa:
open-in-view: false
shopease:
payment:
base-url: https://api.razorpay.com
timeout: 5sExample
application-dev.yml
spring:
datasource:
url: jdbc:h2:mem:shopease
jpa:
hibernate:
ddl-auto: create-drop
logging:
level:
com.shopease: DEBUGExample
application-prod.yml
spring:
datasource:
url: ${DB_URL}
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
hibernate:
ddl-auto: validate
server:
port: ${PORT:8080} # use PORT if set, else 8080Activating a Profile
Example
Any of these works; the first line in the log tells you which profile is active:
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
java -jar target/shopease-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
SPRING_PROFILES_ACTIVE=prod java -jar target/shopease-0.0.1-SNAPSHOT.jarExample output
INFO c.s.ShopEaseApplication : Starting ShopEaseApplication v0.0.1-SNAPSHOT using Java 21.0.4 INFO c.s.ShopEaseApplication : The following 1 profile is active: "prod" INFO o.s.b.w.e.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
Beans can belong to a profile too:
@Bean
@Profile("dev")
PaymentGateway fakeGateway() { return new FakePaymentGateway(); }
@Bean
@Profile("!dev") // every profile except dev
PaymentGateway razorpayGateway(RazorpayProperties props) { return new RazorpayGateway(props); }Note: In tests, activate a profile with @ActiveProfiles("test") on the test class.