This guide explains how to set up
multi-profile configurations in
Spring Boot with
H2 database for different environments.
1. Add Dependencies
In
pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
2. Default Application Properties
In
application.properties
:
spring.profiles.active=dev
This sets the default profile to
dev
.
3. Dev Profile with H2
Create
application-dev.properties
:
spring.datasource.url=jdbc:h2:mem:devdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
4. Test Profile with H2
Create
application-test.properties
:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
5. Switching Profiles
Run the app with a specific profile:
mvn spring-boot:run -Dspring-boot.run.profiles=test
This activates the
test
configuration instead of
dev
.
6. Usage
dev
profile → for local development
test
profile → for integration/unit testing
- other profiles → can later be added for staging or production
image quote pre code