Java 如何使用Spring框架使用YAML文件?

Java 如何使用Spring框架使用YAML文件?,java,spring-mvc,spring-boot,yaml,property-files,Java,Spring Mvc,Spring Boot,Yaml,Property Files,我的应用程序是基于spring的,我想从一个集中式服务器加载yml文件,但服务器无法这样做 我的yml文件如下所示: spring: application: name: signed-in-web cloud: config: uri: ${server.url} health: config: enabled: false @Profile("!test") @ComponentScan(basePackages = { "com.compa

我的应用程序是基于spring的,我想从一个集中式服务器加载yml文件,但服务器无法这样做

我的yml文件如下所示:

spring:
  application:
    name: signed-in-web
  cloud:
    config:
      uri: ${server.url}
health:
  config:
    enabled: false
@Profile("!test")
@ComponentScan(basePackages = { "com.company.frontend.*" })
@Configuration
@PropertySource("classpath:properties/qa_env.properties")
public class propertiesConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("bootstrap.yml"));
            propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    }
}
@Value("${LOCATION_SEARCH}")
public static String LOCATION_SEARCH;
注意:server.url在vm选项中定义。 我已经从rest客户端检查了属性是否有效并且在服务器上可用

然后我尝试将其复制到属性文件中,如下所示: 应用程序属性

LOCATION_SEARCH=${properties.app.api-key.location-search}
我有如下java配置类:

spring:
  application:
    name: signed-in-web
  cloud:
    config:
      uri: ${server.url}
health:
  config:
    enabled: false
@Profile("!test")
@ComponentScan(basePackages = { "com.company.frontend.*" })
@Configuration
@PropertySource("classpath:properties/qa_env.properties")
public class propertiesConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("bootstrap.yml"));
            propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    }
}
@Value("${LOCATION_SEARCH}")
public static String LOCATION_SEARCH;
我想这会像这样加载配置:

spring:
  application:
    name: signed-in-web
  cloud:
    config:
      uri: ${server.url}
health:
  config:
    enabled: false
@Profile("!test")
@ComponentScan(basePackages = { "com.company.frontend.*" })
@Configuration
@PropertySource("classpath:properties/qa_env.properties")
public class propertiesConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("bootstrap.yml"));
            propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    }
}
@Value("${LOCATION_SEARCH}")
public static String LOCATION_SEARCH;

但是我正在变空。不知何故,我无法找到问题所在。将

如果使用Spring Boot,只需调用它
应用程序qa.yaml
并启用
qa
配置文件。另请参见中的
spring.config
spring.profiles

否则,您只需要在
@PropertySource
@PropertySources
中声明yaml文件

只有当您不希望全局环境中的属性时,才开始需要使用
属性配置工厂
YamlPropertiesFactoryBean
手动绑定它们