Tomcat开发环境修改Spring Boot配置默认路径及多配置文件设置
Hey there! Let's work through your two Spring Boot configuration questions step by step—since you're using spring-boot-starter-parent 1.5.10, I'll make sure the answers fit that specific version perfectly.
Spring Boot loads config files from fixed default locations by default (like src/main/resources/, classpath:/config/, etc.). To override or expand this default path, here are the most practical methods:
Command-line arguments (quickest for one-off adjustments):
Specify the path directly when starting your app. If you want to keep the default paths and add new ones, usespring.config.additional-location(supported in 1.5.10) to avoid overwriting default configs:# Replace default paths entirely java -jar your-app.jar --spring.config.location=file:/path/to/your/custom/config/ # Add extra paths while keeping defaults java -jar your-app.jar --spring.config.additional-location=file:/path/to/extra/config/JVM system properties:
Set the path via JVM arguments, useful if you need to embed this in startup scripts:java -Dspring.config.location=file:/path/to/your/config/ -jar your-app.jarProgrammatic configuration:
Customize theSpringApplicationinstance in your main class to hardcode the path (good for fixed dev environments):@SpringBootApplication public class YourAppApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(YourAppApplication.class); app.setDefaultProperties(Collections.singletonMap("spring.config.location", "file:/path/to/your/config/")); app.run(args); } }
For your scenario of splitting config across two properties files, here are three reliable approaches tailored to 1.5.10:
Method 1: Configure via the main application.properties
Add the spring.config.additional-location property to your primary application.properties to specify the second file. Configs from both files will merge (duplicate keys in the second file will override the first):
# Main application.properties spring.datasource.url=url spring.datasource.username=root spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=update # ... your other main configs # Point to the second config file (supports classpath or absolute file paths) spring.config.additional-location=classpath:/datasource-details.properties
Your second file (datasource-details.properties) can hold just the complementary config:
# datasource-details.properties spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.zaxxer.hikari.HikariDataSource
Method 2: Use the @PropertySource annotation
Add this annotation to your main application class or a config class to explicitly load multiple properties files:
@SpringBootApplication @PropertySource(value = {"classpath:/application.properties", "classpath:/datasource-details.properties"}) public class YourAppApplication { public static void main(String[] args) { SpringApplication.run(YourAppApplication.class, args); } }
Note: If using this method, be sure to include the main application.properties to retain default loading behavior. For external files, use the file: prefix (e.g., @PropertySource("file:/opt/config/extra-config.properties")).
Method 3: Command-line argument for deployment
Specify both file paths directly when starting the app, separated by commas:
java -jar your-app.jar --spring.config.location=classpath:/application.properties,file:/path/to/your/second-config.properties
内容的提问来源于stack exchange,提问作者Narges

