1

在我们的 prod 环境中,我们confDir从 tomcat 中的 server.xml 提供参数以获取 .properties 文件,但在开发和测试环境中,我们使用类路径中的属性文件。

像这样

<context:property-placeholder location="${confDir:}/jdbc.properties, ${confDir:}/webservice.properties" order="1" ignore-resource-not-found="true" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:jdbc.properties, classpath:webservice.properties" order="2"/>

现在我想使用util:properties标签加载这些属性文件,如下所示,用于访问它们@Value并检查是否为空,如果为空则分配一个默认值

<util:properties id="classpathProps" location="classpath:jdbc.properties" local-override="false" />
<util:properties id="confDirProps" location="{confDir:}/jdbc.properties" local-override="false" />

问题util:properties是当我们不提供confDir属性时它会抛出异常。

有人可以帮我解决这个问题。我尝试了各种SpEL表达式,例如

location="#{${confDir}?${confDir:}/jdbc.properties:''}"

检查是否confDir为空,但我的试验最终是徒劳的。

4

1 回答 1

1

你不需要SpEL;只需使用普通属性占位符默认值...

<util:properties id="foo" location="${foo:classpath:}props"/>

然后,如果你在没有 foo 属性的情况下运行,你会得到......

class path resource [props] cannot be opened because it does not exist

如果你运行-Dfoo=bar/,你会得到......

class path resource [bar/props] cannot be opened because it does not exist

如果你运行-Dfoo=file:/bar/

/bar/props (No such file or directory)

${foo:classpath:}props中,第一个冒号后的值是在属性foo不存在时使用的替换值。

于 2013-04-18T12:50:03.383 回答