spring 带有数据源的 applicationContext.xml 或 hibernate.cfg.xml。区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12634475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:24:06  来源:igfitidea点击:

applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

springhibernatejakarta-eestruts2

提问by Pirzada

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

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

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

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

Question 2:

问题2:

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

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

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。

internal connection pooling for hibernate =This is number 2 below. Right?

用于休眠的内部连接池 =这是下面的第 2 项。对?

simply configure a datasource which supports connection pooling and inject that into your sessionfactorybean =This is number 1 below. right?

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

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

2# -

Pooling and connection info is in hibernate.cfg.xml

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

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

采纳答案by Ken Chan

Answer 1:

答案 1:

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

两种方法都是一样的。默认情况下,hibernate 从classpath:hibernate.cfg.xmlbuild读取配置SessionFactoryLocalSessionFactoryBean只允许您在内部设置休眠配置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.xmlwill take higher priority such that those values in hibernate.cfg.xmlwill be ignored.

如果在两个文件中都指定了相同的属性,则取决于属性,它会产生上瘾效果,或者 中指定的属性 applicationContext.xml将具有更高的优先级,从而hibernate.cfg.xml忽略 中的值 。

For method 1, annotatedClassesand hibernatePropertiesshould have the addictive effect with the corresponding values in hibernate.cfg.xml. The DBCP dataSouruce in applicationContext.xmlshould cause the related properties in hibernate.cfg.xmlbeing ignored.

对于方法1,annotatedClasses并且hibernateProperties应该与在相应的值的成瘾效果hibernate.cfg.xml。DBCP 数据源中的applicationContext.xml应该导致相关属性hibernate.cfg.xml被忽略。

Answer 2:

答案 2:

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.

对于方法 2 ,如果不指定 的任何属性LocalSessionFactoryBean,则所有休眠配置都由hibernate.cfg.xml. 如果在没有配置连接池hibernate.cfg.xmlHibernate的连接池算法默认情况下使用,这是相当简陋,不打算用于生产系统,甚至性能测试。

回答by Alban

If what you want is to build a session factory, you will get the same result with both approaches. I don't think one can do more than the other.

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

In my opinion, you would use the hibernate.cfg.xmlapproach when you are not using Spring. When JUnit testing your DAOs for example. Not having to build a Spring application context makes your tests run faster.

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

However, when you are using Spring, I think it's a good thing to keep your datasource separated from the session factory. You are using Spring for dependency inject, right? Why not using spring to give your session factory what it needs?

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