如果你想避免使用spring.config.additional-location属性,你可以尝试以下解决方法:
@PropertySource
注解来加载这些配置文件。这样你就不需要使用spring.config.additional-location属性了。示例代码如下:@Configuration
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:custom.properties")
})
public class AppConfig {
//...
}
application-custom.properties
,然后将其放置在classpath下。Spring Boot会自动加载这些文件并覆盖默认的配置。示例代码如下:@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@ConfigurationProperties
注解来绑定自定义配置项。然后在应用程序的启动类中使用@EnableConfigurationProperties
注解来启用该配置类。示例代码如下:@Configuration
@ConfigurationProperties(prefix = "custom")
public class CustomProperties {
private String property1;
private int property2;
// getters and setters
//...
}
@SpringBootApplication
@EnableConfigurationProperties(CustomProperties.class)
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
在这种情况下,你可以在application.properties中设置自定义配置项的值,例如:
custom.property1=value1
custom.property2=42
这些解决方法可以帮助你避免使用spring.config.additional-location属性,同时仍然能够加载和使用自定义的配置项。