【问题标题】:Can't import a property value while using a placeholder使用占位符时无法导入属性值
【发布时间】:2015-01-05 16:58:08
【问题描述】:

我正在使用 Spring Boot。

我在类路径之外的外部文件中声明了属性。

我将它添加到我的一个 XML 文件中:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>file:///d:/etc/services/pushExecuterService/pushExecuterServices.properties</value>
        </list>
    </property>
</bean>

但是,我仍然收到此错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'configuration.serviceId' in string value "${configuration.serviceId}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)

我在这个方法的PropertiesLoaderSupport类中添加了一个断点:

public void setLocations(Resource... locations) {
    this.locations = locations;
}

我注意到这个方法被多次调用,在其中一次我注意到位置参数填充:

URL [file:/d:/etc/services/pushExecuterService/pushExecuterServices.properties]

但是,我仍然收到此错误。

  • 我仔细检查了我的项目,我没有任何额外的 PropertyPlaceholderConfigurer bean(没有检查外部依赖项)

  • 我使用硬编码运行我的应用程序,我可以在 spring-boot 日志中看到 xml 中的参数:

    2015-01-05 18:56:52.902  INFO 7016 --- [           main] 
    o.s.b.f.c.PropertyPlaceholderConfigurer: Loading properties file from
    URL [file:/d:/etc/services/pushExecuterService/pushExecuterServices.properties]`
    

所以我不确定发生了什么。有什么线索吗?

谢谢。

【问题讨论】:

  • 你应该做的一件事是重写你的标题,使它成为一个完整的句子,并修复问题中的拼写和格式问题(虽然我乍一看只看到几个)。
  • 刚刚注意到您使用的是 Spring Boot,您应该将属性添加到 application.properties 您正在与框架对抗而不是使用它。
  • 是的,但我们公司的政策是在位于外部的外部文件中具有特定属性。为什么 spring-boot 会阻止我这样做?

标签: java spring spring-mvc spring-boot


【解决方案1】:

Spring Boot 支持基于 Java 的配置。为了添加配置属性,我们可以将@PropertySource 注解与@Configuration 注解一起使用。

属性可以存储在任何文件中。可以使用 @Value 注释将属性值直接注入到 bean 中:

@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfiguration {
    @Value("${mail.protocol}")
    private String protocol;
    @Value("${mail.host}")
    private String host;
}

@PropertySource 的 value 属性指示要加载的属性文件的资源位置。例如,“classpath:/com/myco/app.properties”或“file:/path/to/file”

但是 Spring Boot 提供了一种使用属性的替代方法,允许强类型 bean 管理和验证应用程序的配置:@ConfigurationProperties

请参阅此博客文章,其中包含使用 @ConfigurationProperties 的示例:http://blog.codeleak.pl/2014/09/using-configurationproperties-in-spring.html

对于@PropertySource 示例,您可以查看这篇文章:http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html

【讨论】:

  • 但是您可以将@PropertySource 用于我的问题中详述的类路径之外的文件?
  • 是的。正如我在答案中所写:“@PropertySource 的 value 属性指示要加载的属性文件的资源位置。例如,“classpath:/com/myco/app.properties”或“file:/path/to /文件“
  • 好吧,我忘了提一件事。我将属性注入到 applicationContext.xml 而不是任何 java 类。我试过@PropertySource("file..) 还是一样的错误
  • 我不知道你的配置,但我做了一个测试。我在 Application 类中定义了 PropertySourcesPlaceholderConfigurer 静态 bean,它读取一些外部文件并在 xml 配置中使用它。我使用以下方法将此配置导入应用程序:@ImportResource("classpath:some-config.xml"),一切正常。也许这会给你一些想法。
猜你喜欢
  • 2014-08-14
  • 2018-06-15
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多