带有数据源的 applicationContext.xml 或 hibernate.cfg.xml.不同之处? [英] applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

查看:16
本文介绍了带有数据源的 applicationContext.xml 或 hibernate.cfg.xml.不同之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想澄清一些困惑.我有 applicationContext.xml.

Want to clear some confusion. I have applicationContext.xml.

问题 1: 1 和 1 之间有什么区别?2. 两者的做法是否相同?

Question 1: Whats the difference between 1 & 2. Are they both same with different approach?

问题 2:

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

I asked question on Spring forum regarding some problem. Onething he mentioned about pooling is below

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

if you need/want to use the internal connection pooling for hibernate I would advice against it and simply configure a datasource which supports connection pooling and inject that into your sessionfactorybean.

休眠的内部连接池=这是下面的第二个.对吗?

只需配置一个支持连接池的数据源并将其注入您的 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

Pooling and connection info is in hibernate.cfg.xml

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

推荐答案

答案 1:

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

Both approaches are the same. By default , hibernate reads the configuration from classpath:hibernate.cfg.xml to build SessionFactory . LocalSessionFactoryBean just allows you to setup hibernate configuration inside applicationContext.xml instead of hibernate.cfg.xml .

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

If a same property is specified in both files , depending on the property , it will has addictive effect or the properties specified in applicationContext.xml will take higher priority such that those values in hibernate.cfg.xml will be ignored.

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

For method 1, annotatedClasses and hibernateProperties should have the addictive effect with the corresponding values in hibernate.cfg.xml . The DBCP dataSouruce in applicationContext.xml should cause the related properties in hibernate.cfg.xml being ignored.

答案 2:

对于方法2,如果没有指定LocalSessionFactoryBean的任何属性,则所有的hibernate配置都由hibernate.cfg.xml指定.如果 hibernate.cfg.xml 中没有配置连接池,默认使用hibernate自己的连接池算法,这是非常初级的,不适合在生产系统中使用,甚至不适合用于性能测试.

For method 2 , if you don't specify any properties of LocalSessionFactoryBean , all the hibernate configurations are specified by the hibernate.cfg.xml. If there are no connection pool configured in hibernate.cfg.xml , hibernate's own connection pooling algorithm is used by default , which is quite rudimentary and not intended for use in a production system, or even for performance testing.

这篇关于带有数据源的 applicationContext.xml 或 hibernate.cfg.xml.不同之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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