如何在弹簧声明性地把(绑定)对象到jndi? [英] how to put(bind) object to jndi in spring declaratively?
问题描述
我们有一个纯粹的独立spring应用程序,我们需要将jdbc datasource放在jndi中。 (我们使用jboss的treecache,它需要的数据源在jndi)。
We have a plain standalone spring application and we need to put jdbc datasource in jndi. (we use jboss treecache and it need datasource to be in the jndi).
一些googling发现大多数的所有jndi查找示例与spring,其中一个对象已经放在jndi(通过tomcat或应用程序服务器等),但我们需要其他:我有一个普通的数据源Spring bean,我注入到其他服务,但我不能注入它到TreeCache,因为它只需要它从jndi。
Some googling found most of all jndi-lookup examples with spring, where an object is already put in jndi (by tomcat or application server etc), but we need otherwise: I have a plain datasource Spring bean, which I inject to other services, but I can't inject it to TreeCache, because it needs it only from jndi.
找到 org.springframework.jndi.JndiTemplate
,可以声明为bean,例如:
Found org.springframework.jndi.JndiTemplate
, which can be declared as bean, e.g.:
<bean id="fsJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.sun.jndi.fscontext.RefFSContextFactory</prop>
<prop key="java.naming.provider.url">file:///c:\windows\temp</prop>
</props>
</property>
</bean>
但没有找到如何与它绑定除了在java代码: fsJndiTemplate .bind(name,obj)
从一些其他bean的init方法。
是否有任何方式以声明方式做?
but not found how to bind with it other than in java code: fsJndiTemplate.bind(name, obj)
from init-method of some other bean.
Is there any way to do it declaratively?
推荐答案
您可以创建一个JndiExporter使用JndiTemplate绑定具有名称的对象映射:
Your can create a JndiExporter which uses a JndiTemplate to bind a map of object with a name:
<bean id="jndiExporter" class="org.xxx.JndiExporter">
<property name="jndiTemplate" ref="jndiTemplate">
<property name="objects">
<map>
<entry key="name1" value="bean1"/>
<entry key="name2" value="bean2"/>
<entry key="name3" value="bean3"/>
<entry key="name4" value="bean4"/>
</map>
</property>
</bean>
您的JndiExporter必须实现BeanFactoryAware才能使用注入的BeanFactory检索spring bean。
Your JndiExporter have to implements BeanFactoryAware to retrieve the spring bean with the injected BeanFactory.
这可能是:)
这篇关于如何在弹簧声明性地把(绑定)对象到jndi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!