Search code examples
springjakarta-eejbossjndi

Convert JNDI DataSource Bean to JEE:jndi-lookup


I have a Spring setting as follows.

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
            <prop key="java.naming.provider.url">jnp://jndi.myURL.com:1099</prop>
            <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
            <prop key="jnp.disableDiscovery">true</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/mysql"/><!-- DataSource Name -->
    <property name="resourceRef" value="false"/>
    <property name="jndiTemplate" ref="jndiTemplate" />
</bean>

I want to convert that in JEE tag format.

I want to define my DataSrouce as

<jee:jndi-lookup jndi-name="jdbc/mysql" resource-ref="false" >
    <jee:environment>
        ....
    </jee:environment>
</jee:jndi-lookup>

Is there a way to define that.


Solution

  • You can share the definition with:

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment" ref="remoteEnv" />
    </bean>
    
    <jee:jndi-lookup id="wcDataSource" jndi-name="jdbc/wc-mysql" resource-ref="false" environment-ref="remoteEnv" />
    
    <util:properties id="remoteEnv">
        <prop key="java.naming.provider.url">jnp://jndi.myURL.me:1099</prop>
        <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
        <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
        <prop key="jnp.disableDiscovery">true</prop>
    </util:properties>
    

    This way you can still refer jndiTemplate elsewhere, if it is needed.