在 PropertyPlaceholderConfigurer 实例中使用
发布于 2024-12-20 09:10:11 字数 886 浏览 4 评论 0 原文

环境:Windows server 2003、Spring 3.0、Tomcat 6

如何在 PropertyPlaceholderConfigurer 中引用 JNDI 属性?

具体来说,我使用 JNDI 来查找表示路径的 java.lang.String 我的网络应用程序所需的属性文件

<jee:jndi-lookup id="mypropsfile1" jndi-name="myPropsFile1" resource-ref="true"/>
<jee:jndi-lookup id="mypropsfile2" jndi-name="myPropsFile2" resource-ref="true"/>

<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
    <array>
        <value>how to use mypropsfile1 here ??</value>
        <value>how to use mypropsfile2 here ??</value>
    </array>
  </property>
</bean>

我的“jee:jndi-lookup”正在工作 AFAIK。我的问题似乎是如何引用 JNDI 资源 在标签对内

提前致谢! 标记

Environment: Windows server 2003, Spring 3.0, Tomcat 6

How can I reference a JNDI property inside a PropertyPlaceholderConfigurer?

Specifically, I'm using JNDI to look up a java.lang.String that represents a path to
a property file needed by my webapp

<jee:jndi-lookup id="mypropsfile1" jndi-name="myPropsFile1" resource-ref="true"/>
<jee:jndi-lookup id="mypropsfile2" jndi-name="myPropsFile2" resource-ref="true"/>

<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
    <array>
        <value>how to use mypropsfile1 here ??</value>
        <value>how to use mypropsfile2 here ??</value>
    </array>
  </property>
</bean>

My "jee:jndi-lookup"s are working AFAIK. My problem seems to be how to reference JNDI resources
inside the tag pair

Thanks in advance!
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2024-12-27 09:10:11

你的方法可能不起作用,马克,这是因为 PropertyPlaceHolderConfigurer 是一个 BeanFactoryPostProcessor,并且在创建 bean 定义时被调用,而 jndi 查找发生在这个阶段之后。

我看到一个 旧版 Spring 论坛 讨论项,其中有关于使用基于 jndi 查找的属性文件的方法的建议,这可能适合您的需求:

Your approach may not work Mark, this is because PropertyPlaceHolderConfigurer is a BeanFactoryPostProcessor and gets invoked at the point when the bean definitions are created, whereas the jndi lookup happens post this stage.

I saw an older Spring forum discussion item, which has a recommendation for an approach of using a jndi lookup based properties file, which may suit your needs:

最单纯的乌龟 2024-12-27 09:10:11

我相信你将不得不做这样的事情。我还没有测试过它,但基本上 PropertyPlaceholderConfigurer 中的 setLocations 方法接受一个 Resource 数组(在我们的例子中 UrlResource - http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/UrlResource.html),它又具有一个构造函数文件路径。

<jee:jndi-lookup id="mypropsfile1" jndi-name="myPropsFile1" default-value="file:///C:/defaultPath" resource-ref="true"/>
<jee:jndi-lookup id="mypropsfile2" jndi-name="myPropsFile2" resource-ref="true"/>

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="mypropsfile1,mypropsfile2">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
        <list>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="mypropsfile1"/></constructor-arg>
           </bean>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="myPropsFile2"/></constructor-arg>
           </bean>
        </list>

  </property>
</bean>

我不确定是否有一个名为 in spring 的标签。检查这个 http:// /static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-introduction

I believe you will have to do something like this . I haven't tested it but basically the setLocations method in PropertyPlaceholderConfigurer takes in an array of Resource(In our case UrlResource - http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/UrlResource.html) which in turn has a constructor with the file path .

<jee:jndi-lookup id="mypropsfile1" jndi-name="myPropsFile1" default-value="file:///C:/defaultPath" resource-ref="true"/>
<jee:jndi-lookup id="mypropsfile2" jndi-name="myPropsFile2" resource-ref="true"/>

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="mypropsfile1,mypropsfile2">
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="locations">
        <list>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="mypropsfile1"/></constructor-arg>
           </bean>
           <bean class="org.springframework.core.io.UrlResource">
               <constructor-arg><ref bean="myPropsFile2"/></constructor-arg>
           </bean>
        </list>

  </property>
</bean>

I am not sure if there is a tag called in spring . Check this http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-introduction

南笙 2024-12-27 09:10:11

我在 Spring 3 中使用地图执行此操作,如下所示:

<jee:jndi-lookup id="myJndiLookup" jndi-name="com.techtrip.spring.config.myJndiLookup"></jee:jndi-lookup>       

<bean id="somethingWithMap" class="com.techtrip.foo.SomethingWithMap">
    <property name="propMap">
        <map>
            <entry key="myJndiLookup" value-ref="myJndiLookup" />
        </map>
    </property>
</bean>

这在大多数情况下工作正常。如果您使用 AOP 或将 bean 包装在 Proxy 类中的东西,即使您正确设置 eager init,您也可能会遇到麻烦。这种情况下的解决方案是在需要时直接从应用程序上下文访问 SomethingWithMap bean:

applicationContext.getBeansOfType(type, includeNonSingletons, allowedEagerInit);

*请注意,这将返回一个 Map< String, T>,您可以在其中按名称访问 bean。

I do this in Spring 3 using a map as follows:

<jee:jndi-lookup id="myJndiLookup" jndi-name="com.techtrip.spring.config.myJndiLookup"></jee:jndi-lookup>       

<bean id="somethingWithMap" class="com.techtrip.foo.SomethingWithMap">
    <property name="propMap">
        <map>
            <entry key="myJndiLookup" value-ref="myJndiLookup" />
        </map>
    </property>
</bean>

This works fine in most cases. You may run into trouble if you use AOP or something that wraps the bean in a Proxy class even if you set eager init correctly. A solution in that case is to directly access the somethingWithMap bean from the app context when needed using:

applicationContext.getBeansOfType(type, includeNonSingletons, allowEagerInit);

*Note that this will return a Map<String, T> where you can access the bean by name.

粉红×色少女 2024-12-27 09:10:11

不完全是针对单个 JNDI 属性,而是使用通过 JNDI 获取的属性引用:

<!-- Lookup Properties reference through JNDI -->
<jee:jndi-lookup id="config-properties" jndi-name="resources/resource-name" resource-ref="true"/>

<bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="config-properties">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <!-- Once the reference is obtained from JNDI, it can be used like any other reference -->
    <property name="properties" ref="config-properties"></property>
</bean>

Not exactly for a single JNDI property, this is using a Properties reference instead, obtained through JNDI:

<!-- Lookup Properties reference through JNDI -->
<jee:jndi-lookup id="config-properties" jndi-name="resources/resource-name" resource-ref="true"/>

<bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="config-properties">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <!-- Once the reference is obtained from JNDI, it can be used like any other reference -->
    <property name="properties" ref="config-properties"></property>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文