小编典典

如何在以声明方式将对象绑定(绑定)到jndi?

spring

我们有一个普通的独立spring应用程序,需要将jdbc数据源放在jndi中。(我们使用jboss treecache,它需要数据源位于jndi中)。

一些谷歌搜索人员发现了大多数使用Spring的jndi查找示例,其中已经在jndi中放置了一个对象(通过tomcat或应用服务器等),但是我们需要另外的方法:我有一个简单的数据源Spring bean,我将其注入到其他服务中,但我无法将其注入TreeCache,因为它仅需要jndi的支持。

Found org.springframework.jndi.JndiTemplate,可以声明为bean,例如:

<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-method。有什么办法声明性地做到这一点?


阅读 546

收藏
2020-04-21

共1个答案

小编典典

你可以创建一个使用JndiTemplate绑定名称的对象映射的JndiExporter:

<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。

2020-04-21