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.propertiesFile- By default, Spring Boot looks for
application.propertiesorapplication.ymlinsrc/main/resourcesto load configuration settings. - To use your custom properties files (
appProperties.propertiesandappProperties-hk.properties), you need to inform Spring Boot to look for files with the base nameappPropertiesinstead of the defaultapplication. -
Add the following line to a new
application.propertiesfile insrc/main/resources:spring.config.name=appProperties - This setting tells Spring Boot to load
appProperties.propertiesas the base configuration file, and it will also automatically look for profile-specific variants likeappProperties-{profile}.propertieswhen a profile is active.
- By default, Spring Boot looks for
- Use Spring Profiles to Specify the
-hkProperties- 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.propertiesandappProperties-hk.properties, with properties inappProperties-hk.propertiesoverriding 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.activeargument. For example:java -jar target/myapp.jar --spring.profiles.active=hkReplace
myapp.jarwith the name of your application’s JAR file generated by Maven. - Via Maven: If you’re running the application using the
spring-boot:rungoal, 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=appPropertiesset inapplication.properties, Spring Boot usesappPropertiesas the base name for configuration files. - When no profile is active, only
appProperties.propertiesis loaded. - When the “hk” profile is activated (e.g., via
--spring.profiles.active=hk), Spring Boot loads bothappProperties.propertiesandappProperties-hk.properties. Properties defined inappProperties-hk.propertiestake precedence over those inappProperties.propertiesif there are any duplicates.
Final Steps
- Ensure both
appProperties.propertiesandappProperties-hk.propertiesare insrc/main/resources. - Add
application.propertieswithspring.config.name=appPropertiestosrc/main/resources. - Run your application with the “hk” profile activated to use the
-hkproperties.
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.