开发者

how to get a spring declared datasource in an xml config file

i have a spring datasource which looks like this:

<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database1.url}" />
    <property name="username" value="${database1.username}" />
    <property name="password" value="${database1.password}" />
</bean>

i need to make this available on jndi or jee. some related things:

<bean id="dataSourceJNDI1" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/dataSource1"/>
    <property name="beanClassLoader" ref="dataSource1"></property>
</bean>

or maybe :

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/database1" />

not sure how to get the jndi or jee working with the dbcp. any help would be appreciated.

thanks in advance.

ps: guys this is a special needl. so i have to do it like this. please dont post unnecessary advices saying why i should use tomcat server as datasource. i am aware of setting datasource that way. i repeat again this is a special need. also please dont provide me java code solutions, not required.

ps: those who dont know how to do it, please do not occupy the space of this post saying its not possible. if you dont 开发者_开发问答know the answer no need to post and junk the post.


You would need to bind the DataSource to the JNDI tree. You would need to supply the parameters for connecting to the local JNDI tree. JndiTemplate can do this. Some of the JNDI environment properties probably won't be necessary for a local InitialContext. I think "java.naming.factory.initial" is the only required. The other are for connecting to an out of process JNDI server:

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.provider.url">${jndi.provider.url}</prop>
            <prop key="java.naming.factory.initial">${jndi.factory.initial}</prop>
            <prop key="java.naming.security.principal">${jndi.security.principal}</prop>
            <prop key="java.naming.security.credentials">${jndi.security.credentials}</prop>
        </props>
    </property>
</bean>

<bean factory-bean="jndiTemplate" factory-method="bind">
    <constructor-arg type="java.lang.String" value="java:com/env/DataSoure"/>
    <constructor-arg type="java.lang.Object" ref="dataSource"/>
</bean>

If you are performing a JNDI lookup in the same Spring context, you will either need to have the JNDI bean depends-on this lookup bean or make the JNDI lookup lazy so that it will perform the lookup on first use.


Since you are using jndi you have to declare the datasource as a jndi source.

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/database1" />

<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="dataSource" ref="dataSource" />
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database1.url}" />
    <property name="username" value="${database1.username}" />
    <property name="password" value="${database1.password}" />
</bean>

That should work assuming your bean definition for dataSource1 is correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜