java - 如何在 Spring Boot linux 中向类路径提供外部配置资源(数据库属性和 XML 文件)

标签 java spring spring-boot

我在 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/

相关文章:

java - jSTL if-else循环参数传递

java - Spring 集成注释 Java 8 可选的不正确处理

java - 如何将 xml 配置 bean 转换为 Java 配置 bean?

java - PagedResources<Resource 和 RestTemplate 不起作用

java - 已删除的实体实例在 Spring boot 单元测试中仍然存在

java - http转换为https进入无限循环

java - 找不到具有 preferredID 的 TopComponent

java - 热部署 Java EAR 以最小化或消除服务器上应用程序的停机时间?

java - 在 Spring 中设置 Soap 请求的 header

java - 配置 Spring Boot 2.0 acuator 框架的安全性