使用 JNDI 配置 hibernate 方言(使用 Spring)
我正在尝试使用 JNDI 而不是属性文件来配置 Spring+Hibernate Web 应用程序。数据库 url、密码、登录名等一切工作正常,但我无法从 JNDI 上下文获取方言。
我在我的datatabase-context.xml中使用这个:
<bean id="databaseUrl" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/url"/>
</bean>
<bean id="databaseDriver" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/driver"/>
</bean>
<bean id="databaseUsername" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/username"/>
</bean>
<bean id="databasePassword" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/password"/>
</bean>
<bean id="databaseDialect" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dialect"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" ref="databaseDriver"/>
<property name="url" ref="databaseUrl"/>
<property name="username" ref=databaseUsername"/>
<property name="password" ref="databasePassword"/>
<property name="maxWait" value="30000"/>
<property name="validationQuery" value="select 1"/>
<property name="maxActive" value="100"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">...</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>-->
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>-->
</props>
</property>
</bean>
问题是我不知道如何在
属性。
有什么想法吗?
I'm trying to configure a Spring+Hibernate web application using JNDI instead of a properties files. Everything works fine por database url, password, login, etc., but I can't get the dialect from the JNDI context.
I'm using this in my datatabase-context.xml:
<bean id="databaseUrl" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/url"/>
</bean>
<bean id="databaseDriver" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/driver"/>
</bean>
<bean id="databaseUsername" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/username"/>
</bean>
<bean id="databasePassword" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/password"/>
</bean>
<bean id="databaseDialect" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dialect"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" ref="databaseDriver"/>
<property name="url" ref="databaseUrl"/>
<property name="username" ref=databaseUsername"/>
<property name="password" ref="databasePassword"/>
<property name="maxWait" value="30000"/>
<property name="validationQuery" value="select 1"/>
<property name="maxActive" value="100"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">...</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>-->
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>-->
</props>
</property>
</bean>
The problem is that I don't know how to use the JNDI value for databaseDialect inside the <prop name="hibernate.dialect">blabla</prop>
property.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring 可以将
Map
转换为Properties
对象,因此您可以执行以下操作:参考:
来源:
PropertiesEditor
javadocSpring can convert a
Map
to aProperties
object, so you can do the following:Reference:
Source:
PropertiesEditor
javadoc