我在application.yaml文件中有以下部分试图读取外部属性文件:
spring:
config:
name: my-config
location: ./config/pom.xml看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myservice</groupId>
<artifactId>my-service</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.everit.json</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<properties>
<java.version>1.10</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Application.java:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}配置类,我试图在其中使用config:
@Component
@ConfigurationProperties(prefix = "my-config-prefix")
@RefreshScope
public class MyConfig {
...当我试图运行Microservice时,它不会从外部配置my-config.yml文件中映射任何配置元素。
如果我将my-config.yml的全部内容复制并粘贴到application.yml文件中,它将按预期工作,映射MyConfig类中的所有元素!
Spring似乎会拾取application.yml文件下的所有元素,例如,如果我更改了server.port属性,我可以在重新启动时看到这个变化,但是看起来它并没有在'spring‘元素下获取任何属性。
我已经尝试在命令行上提供该属性,它按照预期工作,但是我希望在yml文件中提供这个属性:
--spring.config.location=file:///Users/home/my-config.yml此外,我还试图从application.yml文件中指定完整的位置(如上面所示),但它不起作用。
发布于 2019-09-12 07:38:41
我通过将application.yml文件重命名为bootstrap.yml来解决这个问题。
看起来只有引导属性才能访问'spring.*‘值。
https://stackoverflow.com/questions/57891291
复制相似问题