【问题标题】:applicationContext.xml with datasource or hibernate.cfg.xml. Difference?带有数据源的 applicationContext.xml 或 hibernate.cfg.xml。区别?
【发布时间】:2012-09-28 06:07:32
【问题描述】:

想解开一些困惑。我有 applicationContext.xml。

问题 1: 1 和 2 之间有什么区别。它们是否相同但方法不同?

问题 2:

我在 Spring 论坛上就一些问题提出了问题。下面是他提到的关于池化的内容

如果您需要/想要使用内部连接池进行休眠 我会建议反对它并简单地配置一个数据源 支持连接池并将其注入您的 sessionfactorybean。

hibernate 的内部连接池 = 这是下面的第 2 位。对吧?

只需配置一个支持连接池的数据源并将其注入您的 sessionfactorybean = 这是下面的第 1 位。对吧?

1# -

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="100"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="16000"/>
        <property name="minIdle" value="0"/>
    </bean>

 <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>com.mkyong.customer.model.Customer</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.generate_statistics">true</prop>
            </props>
        </property>
    </bean>

2#-

池和连接信息在 hibernate.cfg.xml 中

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

【问题讨论】:

  • 嗨,我也对使用哪种方法感到困惑。我想我会选择 cfg.xml,因为它可以将休眠配置与此 applicationContext 分离并解耦,从而更容易创建多个数据配置源。

标签: spring hibernate jakarta-ee struts2


【解决方案1】:

答案 1:

这两种方法都是一样的。默认情况下,hibernate 从classpath:hibernate.cfg.xml 读取配置来构建SessionFactoryLocalSessionFactoryBean 只允许您在 applicationContext.xml 中设置休眠配置,而不是 hibernate.cfg.xml

如果在两个文件中指定了相同的属性,根据属性的不同,它会产生令人上瘾的效果,或者applicationContext.xml 中指定的属性将具有更高的优先级,从而hibernate.cfg.xml 中的那些值将被忽略。

对于方法 1,annotatedClasseshibernateProperties 应该与 hibernate.cfg.xml 中的对应值具有上瘾效果。 applicationContext.xml 中的 DBCP 数据源应该会导致 hibernate.cfg.xml 中的相关属性被忽略。

答案 2:

对于方法 2,如果不指定 LocalSessionFactoryBean 的任何属性,则所有休眠配置都由 hibernate.cfg.xml 指定。如果在hibernate.cfg.xmlhibernate's own connection pooling algorithm is used by default中没有配置连接池,这是相当初级的,不适合在生产系统中使用,甚至不用于性能测试。

【讨论】:

    【解决方案2】:

    如果您想要构建一个会话工厂,那么这两种方法都会得到相同的结果。我不认为一个人能比另一个人做得更多。

    在我看来,当您不使用 Spring 时,您会使用 hibernate.cfg.xml 方法。例如,当 JUnit 测试您的 DAO 时。不必构建 Spring 应用程序上下文使您的测试运行得更快。

    但是,当您使用 Spring 时,我认为将数据源与会话工厂分开是一件好事。您正在使用 Spring 进行依赖注入,对吗?为什么不使用 spring 为您的会话工厂提供所需的东西?

    【讨论】:

    • 谢谢。 “为什么不使用 spring 来为你的会话工厂提供它所需要的东西”是什么意思?
    • 我的意思是,将数据源注入您的会话工厂,就像您将 dao 注入服务或将服务注入控制器一样。它为您提供相同的好处:您将组件集成在一个地方:应用程序上下文。您需要切换到替代数据源实现,您知道在哪里寻找。您甚至可以使用属性占位符选择要注入的数据源实现。
    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 2016-05-19
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2011-04-08
    相关资源
    最近更新 更多