【问题标题】:java spring context:property-placeholder - set properties paths from placeholderjava spring context:property-placeholder - 从占位符设置属性路径
【发布时间】:2014-06-04 13:16:11
【问题描述】:
<context:property-placeholder
    location="a.properties,b.properties"
    ignore-unresolvable="true"/>

结果:两个属性文件都已加载

<context:property-placeholder
    location="${properties_location}"
    ignore-unresolvable="true"/>

其中 properties_location 是“a.properties,b.properties”

result: Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [a.properties,b.properties] cannot be opened because it does not exist

编辑:${properties_location} 设置如下:

System.getProperties().setProperty("properties_location", "a.properties,b.properties");
ApplicationContext ctx = new GenericXmlApplicationContext("context.xml");
...

如何以第二种方式初始化我的应用程序?在占位符中定义所有属性文件的路径。

【问题讨论】:

  • 你能指定你使用的是哪个spring版本吗?
  • 问题是占位符在StringString[] 转换后得到解决。所以你想要的是,目前,不可能。

标签: java spring properties


【解决方案1】:

您必须将其更改为:

<context:property-placeholder
location="classpath:a.properties,
          classpath:b.properties"
ignore-unresolvable="true"/>

【讨论】:

  • 谢谢。但这并不能回答我的问题 - 如何将这些路径放在占位符中。
【解决方案2】:

来自property-placeholder 元素的解析器源。

String location = element.getAttribute("location");
if (StringUtils.hasLength(location)) {
    String[] locations = StringUtils.commaDelimitedListToStringArray(location);
    builder.addPropertyValue("locations", locations);
}

首先检索位置,如果该位置具有值,则将其转换为String[]。 Springs 转换服务负责替换 String[] 中的所有占位符。但此时properties_location 占位符只是数组中的一个元素,无需进一步处理即可解析为a.properties,b.properties

所以目前我担心占位符是不可能的。

如果 SpEL 始终是系统属性,您可以使用 #{systemProperties['properties_location']} 来解析该值,这可能会起作用。这应该在其他任何事情之前解决。

【讨论】:

  • ${properties_location} 在示例中设置为系统属性。我编辑了我的问题。
【解决方案3】:

您不能将属性占位符用作占位符占位符解析器中的值。就像在说,“嘿,解析所有属性位置的占位符,然后你就可以开始解析属性了!”。

从逻辑上讲,它只是有意义的。我最近在尝试使用 spring 属性占位符解析,并偶然发现了同样的问题。我尝试使用两个属性占位符配置器,一个用于解析第二个属性的位置,第二个用于解析其余属性。当然,由于 spring 初始化它的 bean 的方式,这个剂量是有效的。

  1. 初始化 bean 后处理器
  2. 构造它们
  3. 构造所有其他 bean

由于属性占位符配置器是一个 bean 后处理器,如果您有多个它们,它们会同时初始化和构造,因此在构造时对彼此的属性一无所知

编辑

鉴于属性位置是系统属性,您可以拥有:

System.getProperties().setProperty("properties_location_a", "classpath:/a.properties");
System.getProperties().setProperty("properties_location_b", "classpath:/b.properties");

然后在你的 spring content.xml 中:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
    <list>
      <value>${properties_location_a}</value>
      <value>${properties_location_b}</value>
    </list>
  </property>
</bean>

【讨论】:

  • ${properties_location} 在示例中设置为系统属性。我编辑了我的问题。
  • 是的,这是一个明显的解决方法。问题是我无法在不更改 java 代码的情况下更改属性文件的数量。
  • 所以也许保留 System.getProperties().setProperty("properties_location", "a.properties,b.properties");但是在您的spring上下文中创建一个工厂bean,它采用该属性,将其拆分为“,”并返回一个列表。然后在 PropertyPlaceholderConfigurer 上,有属性 name="locations" ref="listFactoryBean"?
  • 另外,它必须实现和使用 BeanPostProcessor
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 2011-11-24
  • 2014-05-01
  • 1970-01-01
  • 2014-03-23
  • 2014-07-16
  • 1970-01-01
相关资源
最近更新 更多