【发布时间】:2012-11-19 04:02:02
【问题描述】:
我的 Web 应用程序使用 Spring 3.1.2 和 Hibernate 4.1.7。我现在想配置这两个。我有我的hibernate.cfg.xml 文件:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
我的webapp-servlet.xmlspring 配置文件:
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name = "dataSource" ref = "dataSource"></property>
</bean>
<bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
<property name = "url" value = "jdbc:mysql://localhost:3306/test" />
<property name = "username" value = "root" />
<property name = "password" value = "root" />
<property name = "maxActive" value = "10" />
</bean>
</beans>
- 当所有需要的数据都已经包含在休眠配置文件中时,为什么我需要配置一个 DataSource bean? Hibernate 是否有一些可以使用的默认值?
- 还有哪些
DataSources 我可以使用? - 我是否缺少任何其他 bean 或配置参数/属性来让 hibernate 与我的应用程序一起工作?
【问题讨论】:
-
hibernate.cfg.xml中的所有配置选项都可以在LocalSessionFactoryBean上使用,更喜欢后者并在 Hibernate 配置中跳过它们。 -
好的。但我仍然只希望它们在一个地方,而不是在不同的文件或 bean 中重复。
-
这就是我要说的,从
hibernate.cfg.xml中删除数据源配置并将其保留在Spring XML 中。您可以稍后使用相同的dataSourcebean,例如在JdbcTemplate.