2013-07-30 36 views
3

我有2个需要绑定在一起的文件:hibernate.cfg.xml和hibernate属性。 如何使用PropertyPlaceholderConfigurer将它们指向彼此?如果没有将它们声明为bean,它有可能吗?(我是Spring的初学者)。每一个答案是赞赏。PropertyPlaceholderConfigurer with Hibernate.cfg.xml

在此先感谢。

纳扎尔

的hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="db"> 
      <value>hibernate.properties</value> 
     </property> 
    </bean> 
     <property name="hibernate.dialect">${db.dialect}</property> 
     <property name="hibernate.connection.driver_class">${db.driver}</property> 
     <property name="hibernate.connection.url">${db.url}</property> 
     <property name="hibernate.connection.username">${db.username}</property> 
     <property name="hibernate.connection.password">${db.password}</property> 
     <property name="connection.pool_size">${db.pool_size}</property> 
     <property name="current_session_context_class">${db.current_session_context_class}</property> 
     <property name="hibernate.show_sql">${db.show_sql}</property> 
     <property name="hibernate.cache.provider_class">${db.provider_class}</property> 
     <property name="hibernate.cache.use_second_level_cache">${db.use_second_level_cache}</property> 
     <property name="hibernate.cache.use_query_cache">${db.use_query_cache}</property> 
     <property name="hibernate.hbm2ddl.auto">${db.hbm2ddl_auto}</property> 
     <property name="hibernate.hbm2ddl.import_files">${db.import_files}</property> 
     <mapping class="com.dataart.mediaportal.model.User"/> 
     <mapping class="com.dataart.mediaportal.model.Album"/> 
     <mapping class="com.dataart.mediaportal.model.Role"/> 
     <mapping class="com.dataart.mediaportal.model.Image"/> 

    </session-factory> 
</hibernate-configuration> 

hibernate.properties:

db.username=postgres 
db.password=4351 
db.driver=org.postgresql.Driver 
db.url=jdbc:postgresql://localhost/MediaPortalDB 
db.pool_size=1 
db.dialect=org.hibernate.dialect.PostgreSQLDialect 
db.import_files=import.sql 
db.hbm2ddl_auto=create 
db.use_query_cache=true 
db.use_second_level_cache=true 
db.provider_class=org.hibernate.cache.HashtableCacheProvider 
db.show_sql=true 
db.current_session_context_class=thread 

回答

4

是的,你可以访问这两个文件,并使用它们来创建会话工厂。但是不要在你的hibernate配置文件中这样做。我建议在应用程序上下文中执行它,因为首先,您的hibernate.cfg.xml不包含声明bean所需的名称空间,其次是。它需要被上下文配置器读取,以便它可以实例化bean。

在你的应用程序上下文,可以使用hibernate.properties文件这样最后创建数据源..

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
p:location="{location of hibernate properties files}" /> 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    <property name="driverClass" value="${db.driverClassName}"/> 
    <property name="jdbcUrl" value="${db.databaseurl}"/> 
    ....other properties... 
</bean> 

,创建一个会话工厂这样

<beans:bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <beans:property name="dataSource" ref="dataSource" /> 
     <beans:property name="configLocation"> 
      <beans:value>classpath:hibernate.cfg.xml</beans:value> 
     </beans:property> 
     <beans:property name="configurationClass"> 
      <beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value> 
     </beans:property> 

这将产生您可以使用自动装配访问的会话工厂单例实例。

+0

谢谢!我会立刻试一试,并告诉您该解决方案是否有效。 –

+0

这对我有用:)谢谢 –

+0

一定要使用适当的包,如hibernate4.LocalSessionFactoryBean匹配您的hibernate版本。还包括spring-orm依赖。 –