java Spring 项目如何访问 JBoss JNDI 数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13958672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:34:11  来源:igfitidea点击:

Spring project how do I access JBoss JNDI Datasources

javaspringjbossdatasourcejndi

提问by techsjs2012

Below is my current database.xml file for my Spring Project. Can someone please tell me what would have to be changed so I can use a JBoss JNDI datasource in it.. I want to do this so I don't need the config files with the database user and password and url in it.

下面是我当前用于 Spring 项目的 database.xml 文件。有人可以告诉我必须更改哪些内容以便我可以在其中使用 JBoss JNDI 数据源..我想这样做所以我不需要包含数据库用户和密码以及 url 的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                            http://www.springframework.org/schema/jdbc
                            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

                            <!-- 

     Last changed: $LastChangedDate: 2012-11-19 08:53:13 -0500 (Mon, 19 Nov 2012) $
     @author $Author: [email protected] $
     @version $Revision: 829 $

    -->

    <context:property-placeholder location="classpath:app.properties" />

    <context:component-scan base-package="org.uftwf" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />



    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">

        <!-- these are C3P0 properties      --> 
        <property name="acquireIncrement" value="${database.c3p0.acquireIncrement}" />
        <property name="minPoolSize" value="${database.c3p0.minPoolSize}" />
        <property name="maxPoolSize" value="${database.c3p0.maxPoolSize}" />
        <property name="maxIdleTime" value="${database.c3p0.maxIdleTime}" />
        <property name="maxIdleTimeExcessConnections" value="${database.c3p0.maxIdleTimeExcessConnections}" />
        <property name="numHelperThreads" value="${database.c3p0.numHelperThreads}" />
        <property name="unreturnedConnectionTimeout" value="${database.c3p0.unreturnedConnectionTimeout}" />
        <property name="idleConnectionTestPeriod" value="300" />

        <property name="driverClass" value="${database.driver}" />
        <property name="jdbcUrl" value="${database.url}" />
        <property name="user" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>org.uftwf.enrollment.model.Contact</value>
                <value>org.uftwf.enrollment.model.Enrollment</value>
                <value>org.uftwf.enrollment.model.Member</value>
                <value>org.uftwf.enrollment.model.Profile</value>
                <value>org.uftwf.enrollment.model.School</value>

            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
                <prop key="format_sql">${format_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

回答by Tomasz Nurkiewicz

I assume you can configure DataSourcein JBoss. Notice that you have to define its JNDI name in application server configuration. Once you have the name, simply replace your dataSourcebean with:

我假设您可以DataSource在 JBoss 中进行配置。请注意,您必须在应用程序服务器配置中定义其 JNDI 名称。获得名称后,只需将dataSourcebean替换为:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/some-name"/>
</bean>

or shortcut:

或快捷方式:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/some-name" expected-type="javax.sql.DataSource" />