【问题标题】:PropertyPlaceholderConfigurer with Hibernate.cfg.xml带有 Hibernate.cfg.xml 的 PropertyPlaceholderConfigurer
【发布时间】:2013-07-30 03:30:37
【问题描述】:

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

提前致谢。

纳扎尔

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

【问题讨论】:

    标签: java xml hibernate properties configuration


    【解决方案1】:

    是的,您可以访问这两个文件并使用它们来创建会话工厂。但不是在您的休眠配置文件中执行此操作。我建议在应用程序上下文中执行此操作,因为首先,您的 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>
    

    这将为您创建一个可以使用自动装配访问的会话工厂单例实例。

    【讨论】:

    • 谢谢!稍后我会试一试,如果该解决方案有效,请告诉您。
    • 一定要使用合适的包,比如与你的hibernate版本相匹配的hibernate4.LocalSessionFactoryBean。还包括 spring-orm 依赖项。
    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2013-01-24
    相关资源
    最近更新 更多