You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

1. 修改Spring Boot配置文件的默认路径

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, use spring.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.jar
    
  • Programmatic configuration:
    Customize the SpringApplication instance 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);
        }
    }
    
2. Load a second *.properties file (1.5.10 version)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:42:03