2

我正在尝试使用 spring 将配置外部化,但无法使其正常工作..

这是我到目前为止所做的:

在每个环境的 war 文件 (src/test/resources/) 中创建一个属性文件。例如:nonprod-key.properties & prod-key.properties,内容如下:

    key.name=NameOfPrivateKey.pfx
    key.password=JustAPasswordForPrivateKey

然后在我的 jboss-cxf.xml 中,我想读取上面的值如下:

    <import resource="#{systemProperties['environment']}-key.properties" />

    <http:conduit name="*.http-conduit">
        <http:tlsClientParameters
            secureSocketProtocol="SSL">
            <sec:keyManagers keyPassword="${key.password}">
                <sec:keyStore type="PKCS12" password="${key.password}" resource="${key.name}" />
            </sec:keyManagers>
            ...  ... ...
        </http:tlsClientParameters>
    </http:conduit>

然后在 eclipse 中,运行配置 --> Arguments --> VM Arguments

    -Denvironment=nonprod

不幸的是,上述方法不起作用。:(

我收到此错误消息:

    class path resource [#{systemProperties['environment']}-key.properties] cannot be opened because it does not exist

我试图使用这里的建议:http: //forum.springsource.org/showthread.php?98988 -Access-external-properties-file-from-SPRING-context-file&p=332278#post332278

但似乎无法让它发挥作用。我究竟做错了什么?有人可以举例说明如何最好地做到这一点。

谢谢你。

-SGB

4

2 回答 2

2

我相信需要在 Spring 3.1.x 上才能使用配置文件。我们还没有……。

无论如何,似乎对我们有用的最终解决方案是使用:

<context:property-placeholder location="classpath:${environment}-key.properties"/>

代替

<import resource="#{systemProperties['environment']}-key.properties" />

其他一切都与我原来的帖子(问题)中列出的相同。

希望有人觉得这很有用。

新加坡国标

于 2013-09-13T21:07:52.133 回答
0

您可以使用属性占位符。如果您想要灵活的配置,例如。存储在您的战争中的默认配置可以被外部配置覆盖,您可以直接使用 PropertyPlaceholderConfigurer bean,例如:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:ignoreResourceNotFound="true">
    <property name="locations">
        <array>
            <bean class="org.springframework.core.io.ClassPathResource" c:path="${environment}-key.properties"/>
            <bean class="org.springframework.core.io.FileSystemResource" c:path="relative/path"/>
        </array>
    </property>
</bean>

路径属性可以使用 SPEL 例如引用属性或系统环境变量。

看看这篇文章和这篇如何在 Spring applicationContext 中读取系统环境变量

于 2013-09-13T21:22:41.520 回答