Java 如何在spring中外部化配置?
我试图使用spring将配置外部化,但无法使其正常工作 以下是我迄今为止所做的: 为每个环境在war文件(src/test/resources/)内创建一个属性文件。 例如:包含以下内容的nonprod-key.properties和prod-key.properties:Java 如何在spring中外部化配置?,java,spring,configuration,Java,Spring,Configuration,我试图使用spring将配置外部化,但无法使其正常工作 以下是我迄今为止所做的: 为每个环境在war文件(src/test/resources/)内创建一个属性文件。 例如:包含以下内容的nonprod-key.properties和prod-key.properties: key.name=NameOfPrivateKey.pfx key.password=JustAPasswordForPrivateKey 然后在我的jboss-cxf.xml中,我想阅读上面的值,如下所示
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>
不幸的是,上述方法不起作用:(
我收到以下错误消息:
class path resource [#{systemProperties['environment']}-key.properties] cannot be opened because it does not exist
我试图使用这里的建议:
但似乎无法让它发挥作用。我做错了什么?
有人能举个例子/例子说明如何最好地完成这一点吗
多谢各位
-SGB我认为需要在Spring3.1.x上才能使用配置文件。我们还没有 无论如何,似乎对我们有效的最终解决方案是使用:
<context:property-placeholder location="classpath:${environment}-key.properties"/>
而不是
<import resource="#{systemProperties['environment']}-key.properties" />
其他一切都和我原来的帖子(问题)中列出的一样
希望有人觉得这个有用
SGB您可以使用Property Place Holder。如果您想要灵活的配置,例如war中存储的默认配置,可以被外部配置覆盖,您可以直接使用PropertyPlaceHolderConfigure 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>
显然,模板不能转换为适当的资源名称。请考虑使用配置文件代替您的方法。HM。是否有一个示例代码我可以使用?听起来好像可以从这个链接:我想您需要一个<代码> PrimeTyPyHeldDealSuffer-<代码>来工作。
<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>