如何将hibernate.cfg.xml创建为数据源bean [英] How to make hibernate.cfg.xml to a datasource bean
问题描述
我有一个hibernate.cfg.xml文件,但我想将它作为一个dataSource bean引用。有没有办法做到这一点?我所拥有的是:
< beans:bean id =fooclass =org.springframework.orm.hibernate3。 LocalSessionFactoryBean的>
< beans:property name =configLocationvalue =classpath:hibernate.cfg.xml/>
但它不是一个dataSource。任何帮助将不胜感激。
为了添加一些上下文,我想将Spring Security集成到我的应用程序中,以及将数据库提供为身份验证管理器的源代码如下(摘自 here ):
< authentication-manager>
< authentication-provider>
< jdbc-user-service data-source-ref =securityDataSource/>
< / authentication-provider>
< / authentication-manager>
其中securityDataSource是应用程序上下文中的DataSource bean的名称,指向数据库。
您有两个选择:
$ b $ ol <
<如果你没有在其他任何地方使用 hibernate.cfg.xml
,你可以完全消除它,只使用Spring的配置(见下文)
示例配置(注意数据库访问在单独的 dataSource
bean中)
< beans:bean id =fooclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.hbm2ddl.auto> create-drop< / prop>
...
< / property>
< property name =packagesToScan>
< list>
<值> some.package< /值>
< / list>
< / property>
< property name =dataSourceref =c3p0DataSource/>
< bean id =c3p0DataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource
scope =singleton
destroy-method =close> ;
< property name =driverClass>
< value> org.postgresql.Driver< / value>
< / property>
< property name =jdbcUrl>
< value> $ {jdbc.url}< /值>
< / property>
< property name =user>
< value> $ {jdbc.user}< /值>
< / property>
< property name =password>
< value> $ {jdbc.pw}< /值>
< / property>
< / bean>
您可以在 http://static.springsource.org/spring/docs/current/spring-framework-reference/ html / orm.html
I have a hibernate.cfg.xml file, but I'd like to reference it as a dataSource bean. Is there any way to do this? All I have is :
<beans:bean id="foo" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<beans:property name="configLocation" value="classpath:hibernate.cfg.xml"/>
but its not a dataSource. Any help would be greatly appreciated.
To add a bit of context, I'd like to integrate Spring Security in my app and one of the ways to provide a database as a source for the authentication manager is as follows (taken from here):
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="securityDataSource"/>
</authentication-provider>
</authentication-manager>
Where "securityDataSource" is the name of a DataSource bean in the application context, pointing at a database.
You have two options:
If you're not using the
hibernate.cfg.xml
anywhere else, you can eliminate it altogether and just use Spring's configuration (see below)If you need
hibernate.cfg.xml
for some reason (you're using it elsewhere), then keep that file and add Spring configuration
Example configuration (notice database access is in a separate dataSource
bean)
<beans:bean id="foo" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
...
</property>
<property name="packagesToScan">
<list>
<value>some.package</value>
</list>
</property>
<property name="dataSource" ref="c3p0DataSource" />
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
scope="singleton"
destroy-method="close">
<property name="driverClass">
<value>org.postgresql.Driver</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.pw}</value>
</property>
</bean>
You can see more information about the configuration options in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/orm.html
这篇关于如何将hibernate.cfg.xml创建为数据源bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!