在 PropertyPlaceholderConfigurer 实例中使用
环境: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 资源
在标签对内
提前致谢!
标记
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
环境: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 资源 在标签对内
提前致谢! 标记
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的方法可能不起作用,马克,这是因为 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:
我相信你将不得不做这样的事情。我还没有测试过它,但基本上 PropertyPlaceholderConfigurer 中的 setLocations 方法接受一个 Resource 数组(在我们的例子中 UrlResource - http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/UrlResource.html),它又具有一个构造函数文件路径。
我不确定是否有一个名为 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 .
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
我在 Spring 3 中使用地图执行此操作,如下所示:
这在大多数情况下工作正常。如果您使用 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:
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.不完全是针对单个 JNDI 属性,而是使用通过 JNDI 获取的属性引用:
Not exactly for a single JNDI property, this is using a Properties reference instead, obtained through JNDI: