Dominic Philip
Dominic Philip

Reputation: 108

Migration from Spring 2.5 to 4

I want to migrate my project from Spring 2.5 to Spring 4.1
The old versions of Spring and Hibernate jars used in my project are:

spring 2.5.6
spring-webmvc 2.5.6
spring-webflow 2.0.7.RELEASE
hibernate-annotations 3.4.0.GA
hibernate-commons-annotations 3.1.0.GA
hibernate-core 3.3.1.GA
hibernate ejb3-persistence 1.0.2.GA

I wanted to know if there is a way I could find the latest version of Hibernate which is compatible with Spring ie 4.1
Secondly I would like to know what configuration changes should I make in applicationContext.xml. My applicationContext looks like this

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location" value="classpath:jdbc.properties"/>
          <property name="ignoreUnresolvablePlaceholders" value="true"/>
   </bean>   

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${dataSource.driverClassName}"></property>
        <property name="url" value="${dataSource.url}"></property>
        <property name="username" value="${dataSource.username}"></property>
        <property name="password" value="${dataSource.password}"></property>
        <property name="initialSize" value="1"></property>
        <property name="maxActive" value="10"></property>
        <property name="maxIdle" value="14"></property>
        <property name="minIdle" value="2"></property>
        <property name="maxWait" value="15000"></property>
        <property name="validationQuery" value="SELECT 1"></property>
        <property name="minEvictableIdleTimeMillis" value="5000"></property>
        <property name="testOnBorrow" value="true"></property>
        <property name="testOnReturn" value="true"></property>
        <property name="removeAbandoned" value="true"></property>
        <property name="removeAbandonedTimeout" value="5"></property>
    </bean>

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- Limit uploads to small (5KB) files for this sample -->
        <property name="maxUploadSize" value="809000" />
  </bean>

    <!-- Default Connection -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>/WEB-INF/hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${dataSource.dialect}</prop>
                <prop key="hibernate.default_batch_fetch_size">30</prop>
                <prop key="hibernate.jdbc.fetch_size">20</prop>
                <prop key="org.hibernate.cache">info</prop>
                <prop key="org.hibernate.transaction">debug</prop>
                <prop key="hibernate.jdbc.batch_size">100</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
            </props>
        </property>
        <property name="schemaUpdate" value="false" />

    </bean>

  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
  </bean>

  <tx:annotation-driven transaction-manager="txManager"/>

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
  </bean>

 <bean id="baseService" abstract="true" lazy-init="true">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="sessionFactory" ref="sessionFactory"/>

  </bean>
</beans>

and at last, I'm using HibernateTemplate for my queries. Can I continue using the same after migration.?

Many thanks for every tip in advance...

Upvotes: 2

Views: 9902

Answers (1)

M. Deinum
M. Deinum

Reputation: 124526

In theory it should be a drop in replace. However before migrating I strongly suggest you first change the xml files you use. Currently you have versioned xsd in your headers i.e. spring-beans-2.5.xsd remove the versions i.e. spring-beans.xsd.

Next your PropertyPlaceHolderConfigurer is best replaced with a <context:property-placeholder /> instead of the plain bean.

The hibernate integration has been highly refactored for Hibernate4 so I would stick with the latest hibernate 3.x version (3.6.10) and make that a separate migration (to either plain Hibernate 4 or JPA). If you have it running again then upgrade to the latest hibernate version (4.3.6) as that will require some code changes and depending on the amount of code can hurt considerably.

You are using the spring jar which doesn't exists anymore so you need to figure out which modules you need (judging from your setup at least jdbc and orm). I really hope that you are using Maven to manage your dependencies else you are in for a treat looking for the correct bundle of related dependencies.

Spring Web Flow will also require an upgrade to the latest 2.4.0, not sure if that is a drop in replacement though.

After updating I guess most of it will still work (or you must have some other unlisted dependencies here that also require an upgrade).

One thing to keep in mind is that also the minor java versions got upgraded so when upgrading to Spring 4.x your project needs to be at least java 1.6 if you are still on 1.5 or lower it will not work.

I also strongly recommend reading the migration guide which contains some valuable information. (You might want to skim through the history of the document to retrieve the 2.5 -> 3.x version).

Upvotes: 3

Related Questions