从另一个属性的属性占位符位置 [英] property-placeholder location from another property

查看:86
本文介绍了从另一个属性的属性占位符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个我不知道的位置将一些属性加载到Spring上下文中,直到程序运行。

I need to load some properties into a Spring context from a location that I don't know until the program runs.

所以我认为如果我有一个没有位置的PropertyPlaceholderConfigurer,它将从系统中读入 my.location 属性,然后我可以在上下文中使用该位置:property-placeholder

So I thought that if I had a PropertyPlaceholderConfigurer with no locations it would read in my.location from the system properties and then I could use that location in a context:property-placeholder

喜欢这个

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>    
<context:property-placeholder location="${my.location}"/>

但这不起作用, location =classpath:$ {my.location}

保罗

推荐答案

这里的问题是你正在尝试使用属性占位符语法配置属性占位符:)这有点像鸡蛋和鸡蛋的情况 - spring无法解析你的 $ {my.location} 占位符,直到它配置了property-placeholder。

The problem here is that you're trying to configure a property place holder using property placeholder syntax :) It's a bit of a chicken-and-egg situation - spring can't resolve your ${my.location} placeholder until it's configured the property-placeholder.

这不太令人满意,但你可以通过使用更多来避免它显式语法:

This isn't satisfactory, but you could bodge it by using more explicit syntax:

<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
   <property name="location">
      <bean class="java.lang.System" factory-method="getenv">
         <constructor-arg value="my.location"/>
      </bean>
   </property>
</bean>

这篇关于从另一个属性的属性占位符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆