冬眠与春季融为一体;避免重复连接参数 [英] Integration of hibernate with spring; Avoid duplication of connection parameters
问题描述
我想在Spring中配置休眠...为此我在spring-servlet.xml
I want to configure hibernate in spring...for that I am using following configurations in spring-servlet.xml
<context:property-placeholder location="classpath:resources/database.properties" />
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>
这是我的database.properties文件
here is my database.properties file
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://10.2.5.142:3306/testdb
database.user=root
database.password=
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
现在我只想使用SessionFactory的单个实例;为此,我在DAO类中添加了以下代码
Now I want to use only single instance of SessionFactory ; for that I included following code in my DAO class
SessionFactory sessionFactory = new Configuration().configure("/resources/hibernate.cfg.xml").buildSessionFactory();
我必须在两个地方设置与数据库相关的参数
I must have to set Database related parameters at two places
(1) database.properties
(2) hibernate.cfg.xml
有什么方法可以将这些值仅放在一个地方
Is there any way by which I can put those values only at single place
推荐答案
您根本不需要hibernate.cfg.xml
文件.您可以将SessionFactory
配置为Spring
bean.
You don't need the hibernate.cfg.xml
file at all. You can configure your SessionFactory
as a Spring
bean.
这里是一个例子:
persistence-hibernate.xml
persistence-hibernate.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:resources/database.properties"/>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${database.driver}"
p:url="${database.url}"
p:username="${database.user}"
p:password="${database.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="package.with.your.entities">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txnManager"/>
<bean id="txnManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
</beans>
您可以使用import标记在spring-servlet.xml
中导入该配置.
You can import that configuration in your spring-servlet.xml
by using the import tag.
<import resource="persistence-hibernate.xml"/>
然后,注入您的SessionFactory而不是自己实例化它.
And then, inject your SessionFactory instead of instantiate it by yourself.
@Repository
@Transactional
public class YourDaoImpl implements YourDao {
@Autowired
private SessionFactory sessionFactory;
...
}
这篇关于冬眠与春季融为一体;避免重复连接参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!