我在 Spring Boot 中一直面临着将外部配置文件添加到类路径的问题,但它没有从外部配置文件夹中选择 application.properties、application-dev 属性。它从外部配置文件夹中选择数据库属性和 XML 文件。我已经尝试了以下方法,您能帮我解决这个问题吗?
java -cp ./config/;./lib/ips--0.0.1-SNAPSHOT.jar java -cp ./config/;./lib/ips-rest-0.0.1-SNAPSHOT.jar java -jar ./lib/ips-rest-0.0.1-SNAPSHOT.jar --spring.config.location=classpath:/config/,file:./config/,classpath:/,file:./
@ImportResource("classpath:ips-spring.xml")
@SpringBootApplication(exclude = { KafkaAutoConfiguration.class })
@ComponentScan(value = "com.mark", useDefaultFilters = false)
@EnableAutoConfiguration
@EnableConfigurationProperties
public class ApplicationRest {
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(ApplicationRest.class, args);
System.out.println("Started ApplicationRest");
}
}
<context:property-placeholder location="classpath:env.properties,classpath:db.properties" ignore-resource-not-found="false" ignore-unresolvable="false" />
<import resource="classpath:app-entity.xml" />
最佳答案
我假设“外部配置”是指未打包到 Spring Boot 工件中的配置文件(*.properties
、*.yml
等)(你的例子中的 jar )
在这种情况下,根据定义,它们不在应用程序的类路径
中。
换句话来说,您是在问如何向 Spring 应用程序提供外部配置文件。正如您已经发现的,确实 --spring.config.location
是正确的选择:
--spring.config.location=file:/work/config1.yml,file:/work/config2.yml
关于java - 如何在 Spring Boot linux 中向类路径提供外部配置资源(数据库属性和 XML 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783227/