PropertyPlaceholderConfigurer with Hibernate.cfg.xml

前端 未结 1 343
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 21:00

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 pos

相关标签:
1条回答
  • 2020-12-19 21:26

    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.

    0 讨论(0)
提交回复
热议问题