2

我的应用程序是基于弹簧的,我想从中央服务器加载 yml 文件,但无法这样做。

我的 yml 文件是这样的:

spring:
  application:
    name: signed-in-web
  cloud:
    config:
      uri: ${server.url}
health:
  config:
    enabled: false

注意: server.url 在 vm 选项中定义。我从休息客户端检查了属性在服务器上是否有效且可用。

然后我尝试将它复制到这样的属性文件中:application.properties

LOCATION_SEARCH=${properties.app.api-key.location-search}

我有这样的java配置类:

@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;

但我越来越空了。不知何故,我无法找到问题所在。将要

4

1 回答 1

1

如果您使用的是 Spring Boot,您只需要调用它application-qa.yaml并启用qa配置文件。另请参阅spring.config常见应用程序属性spring.profiles

否则你只需要在@PropertySourceor中声明 yaml 文件@PropertySources

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

于 2017-03-06T12:59:32.100 回答