【问题标题】:Load jndi resources into context:property-placeholder将 jndi 资源加载到 context:property-placeholder
【发布时间】:2011-07-25 08:57:02
【问题描述】:

我有以下 spring 配置文件:

<context:property-placeholder order="2"
    ignore-unresolvable="true" ignore-resource-not-found="true"
    location="file:///${user.home}/application.properties" />
<context:property-placeholder order="1"
    ignore-unresolvable="true" ignore-resource-not-found="true"
    location="file:///C:/Services/Tomcat 6.0/cms/application.properties" />
<context:property-placeholder order="3"
    location="classpath:com/afrozaar/cms/service/application.properties" />

注意它们是如何排序的,有些在类路径上,有些在文件系统上。

现在我想添加一个通过 jndi 加载的属性文件。我希望能够做到

<context:property-placeholder order="2"
    ignore-unresolvable="true" ignore-resource-not-found="true"
    location="jndi:url/application.properties" />

不幸的是,这不起作用,spring 不支持 jndi 前缀... AFAIK。

那么,我可以这样做吗?

如果我做不到,我的选择是什么。我不想将我的整个配置转换为一个完整的基于 bean 的属性占位符配置器。

【问题讨论】:

    标签: spring jndi


    【解决方案1】:

    不确定“jndi:url/application.properties”的真正含义。我想您想在名为“url/application.properties”的资源条目中设置属性文件的路径。

    您可以使用以下 sn-ps 实现此目的:

    <bean class="org.springframework.beans.factory.config.PlaceholderConfigurerSupport">
     <property name="location">
      <bean id="publisherLocal" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="url/application.properties" />
       <property name="expectedType" value="java.lang.String" />
       </bean>
     </property>
    </bean>
    

    【讨论】:

    • 我希望能够通过在资源上使用“jndi:”前缀来规避这种情况,就像使用类路径:前缀一样。
    【解决方案2】:

    &lt;context:property-placeholder&gt; 有一个properties-ref 属性,可以指向Properties 类型的bean。因此,您可以在代码中加载Properties 并使用它们声明&lt;context:property-placeholder&gt;

    【讨论】:

      猜你喜欢
      • 2012-11-23
      • 2013-06-22
      • 2011-11-24
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2013-12-04
      相关资源
      最近更新 更多