【问题标题】:Environment variables not converted to system properties未转换为系统属性的环境变量
【发布时间】:2020-08-13 13:46:34
【问题描述】:

在基于传统 Spring 的应用程序(Spring 4.3)上工作时,我有一个奇怪的行为:环境变量没有被 Spring 解析。 例如我有这个环境变量:HOST_SERVICE_BASE_URL,当我在应用程序中使用${host.service.base.url} 引用它时,属性未解析,应用程序在启动过程中失败。

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'host.service.base.url' in value "${host.service.base.url}

我为属性解析定义了这些 bean:

    @Bean
    public PropertiesFactoryBean applicationProperties( ResourceLoader resourceLoader ) {
        PropertiesFactoryBean propertiesFactory = new PropertiesFactoryBean();
        propertiesFactory.setLocations( resourceLoader.getResource( "/WEB-INF/config/application.properties" ),
                    resourceLoader.getResource( "/WEB-INF/config/application-dev.properties" ) );
        return propertiesFactory;
    }

    <bean id="dataConfigPropertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="searchSystemEnvironment" value="true"/>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="properties" ref="applicationProperties"/>
    </bean>

【问题讨论】:

  • 环境变量永远不会转换为系统属性。你真的有一个环境变量或者你有一个系统属性(通过-D)。另外,为什么 XML 和 Java 配置的混合以及你是否有多个 PropertyPlaceholderConfigurer 实例并且你正在加载多个配置?
  • @M.Deinum 我有环境变量而不是系统属性。与 xml 混合是因为它是一个遗留应用程序,我不想迁移所有配置
  • 啊已经看到你的问题了。您应该使用 PropertySourcesPlaceholderConfigurer 而不是 PropertyPlaceholderConfigurer 并加载属性,除非您需要它们,否则应该使用相同的类。
  • 是的,加载了多个配置

标签: java spring environment-variables system-properties


【解决方案1】:

您正在使用(现已弃用)PropertyPlaceholderConfigurer,虽然它可以咨询系统环境,但它缺乏将这些属性映射到值的功能。即 HOST_SERVICE_BASE_URL 未映射为 host.service.base.url

该支持仅在 SystemEnvironmentPropertySource 中可用,使用 PropertySourcesPlaceholderConfigurer 时会自动注册和咨询(建议从 Spring 5.2 开始,但从 3.1 开始提供)。

<bean id="dataConfigPropertyConfigurer"
          class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="properties" ref="applicationProperties"/>
</bean>

或者在 Java 中替换 applicationProperties bean 和 XML 部分。

@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
  PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
  configurer.setLocations({"/WEB-INF/config/application.properties", /WEB-INF/config/application-dev.properties"});
  configurer.setIgnoreResourceNotFound(true);
  return configurer;
}

或者,如果您真的坚持使用 XML,请使用 &lt;context:property-placeholder /&gt; 标签,它会自动执行此操作。

<context:property-placeholder properties="applicationProperties" ignore-resource-not-found="true" />

<context:property-placeholder locations="/WEB-INF/config/application.properties,/WEB-INF/config/application-dev.properties" ignore-resource-not-found="true" />

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 2016-11-28
    • 2019-08-14
    • 1970-01-01
    • 2012-12-11
    • 2020-04-03
    • 2015-03-16
    相关资源
    最近更新 更多