PropertyPlaceholderConfigurer与Hibernate.cfg.xml [英] PropertyPlaceholderConfigurer with Hibernate.cfg.xml

查看:218
本文介绍了PropertyPlaceholderConfigurer与Hibernate.cfg.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件需要绑定在一起:hibernate.cfg.xml和hibernate属性。
我如何使用PropertyPlaceholderConfigurer将它们指向对方?有可能没有声明他们是一个豆子(我是春天的初学者)。



提前感谢。



Nazar



hibernate.cfg.xml:

 <?xml version =1.0encoding =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.Roles/>
< 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

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



在应用程序上下文中,您可以使用hibernate.properties文件创建数据源像这样..

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

< bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource>
< property name =driverClassvalue =$ {db.driverClassName}/>
< property name =jdbcUrlvalue =$ {db.databaseurl}/>
....其他属性...
< / bean>

最后,创建一个像这样的会话工厂

 < beans:bean id =sessionFactory
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< bean:property name =dataSourceref =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>

这将为您创建一个会话工厂单例实例,可以使用Autowiring访问。


I have 2 files which need to be bound together: hibernate.cfg.xml and hibernate properties. How can I point them to each other using PropertyPlaceholderConfigurer? Is it possible without declaring them as a beans?(I'm beginner in Spring). Every answer is appreciated.

Thanks in advance.

Nazar

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

解决方案

Yes, you can access both the files and use them to create the Session Factory. But instead of doing this inside your hibernate configuration file. I would suggest to do it inside the application context because first, your hibernate.cfg.xml does not contain the name space required to declare the bean and secondly. it needs to be read by the context configurer so, that it can instantiate the bean.

In you application context, you can create a data source using the hibernate.properties file like this..

<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>

Finally, create a session factory like this

<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>

This would create a session factory singleton instance for you which could be accessed using Autowiring.

这篇关于PropertyPlaceholderConfigurer与Hibernate.cfg.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆