Property File Selection in Spring Boot | Generated by AI
In a Spring Boot Maven project, you have two properties files, appProperties.properties
and appProperties-hk.properties
, located under src/main/resources
. To control which properties file is used, particularly to specify the use of the -hk
properties, you can leverage Spring Boot’s configuration mechanisms. Here’s how you can do it:
Solution
- Create an
application.properties
File- By default, Spring Boot looks for
application.properties
orapplication.yml
insrc/main/resources
to load configuration settings. - To use your custom properties files (
appProperties.properties
andappProperties-hk.properties
), you need to inform Spring Boot to look for files with the base nameappProperties
instead of the defaultapplication
. -
Add the following line to a new
application.properties
file insrc/main/resources
:spring.config.name=appProperties
- This setting tells Spring Boot to load
appProperties.properties
as the base configuration file, and it will also automatically look for profile-specific variants likeappProperties-{profile}.properties
when a profile is active.
- By default, Spring Boot looks for
- Use Spring Profiles to Specify the
-hk
Properties- Spring Boot supports profiles, which allow you to load additional or overriding properties files based on the active profile.
- Since your file is named
appProperties-hk.properties
, it follows the patternappProperties-{profile}.properties
. Here, “hk” can be treated as a profile name. - To use
appProperties-hk.properties
, activate the “hk” profile when running your application. Spring Boot will then load bothappProperties.properties
andappProperties-hk.properties
, with properties inappProperties-hk.properties
overriding any matching properties inappProperties.properties
.
- How to Activate the “hk” Profile
- Via Command Line: When running your Spring Boot application, specify the active profile using the
--spring.profiles.active
argument. For example:java -jar target/myapp.jar --spring.profiles.active=hk
Replace
myapp.jar
with the name of your application’s JAR file generated by Maven. - Via Maven: If you’re running the application using the
spring-boot:run
goal, configure the profile in yourpom.xml
:<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <profiles> <profile>hk</profile> </profiles> </configuration> </plugin>
Then run:
mvn spring-boot:run
- Via System Property: Set the profile as a JVM argument:
java -Dspring.profiles.active=hk -jar target/myapp.jar
- Via Environment Variable: Export the profile in your environment (e.g., in a Unix-like system):
export SPRING_PROFILES_ACTIVE=hk java -jar target/myapp.jar
- Via Command Line: When running your Spring Boot application, specify the active profile using the
How It Works
- With
spring.config.name=appProperties
set inapplication.properties
, Spring Boot usesappProperties
as the base name for configuration files. - When no profile is active, only
appProperties.properties
is loaded. - When the “hk” profile is activated (e.g., via
--spring.profiles.active=hk
), Spring Boot loads bothappProperties.properties
andappProperties-hk.properties
. Properties defined inappProperties-hk.properties
take precedence over those inappProperties.properties
if there are any duplicates.
Final Steps
- Ensure both
appProperties.properties
andappProperties-hk.properties
are insrc/main/resources
. - Add
application.properties
withspring.config.name=appProperties
tosrc/main/resources
. - Run your application with the “hk” profile activated to use the
-hk
properties.
This approach aligns with Spring Boot conventions, making it a typical and straightforward way to control which properties file is used in your project. To specifically use the -hk
properties, simply activate the “hk” profile as described above.