Spring2.5.6+Hibernate3.0.5升级到Spring3.0.5+Hibernate3.3

来源:互联网 发布:动力it 编辑:程序博客网 时间:2024/05/21 17:52

SSH框架升级

Spring2.5.6+Hibernate3.0.5升级到Spring3.0.5+Hibernate3.3

1、引言

项目使用的是Struts1 + Spring2.5.6 + Hibernate3.0.5框架,但项目需要开发Flex客户端, Spring 2.5.6不支持与Flex集成,于是决定升级Spring至 3.0.5版本 ,升级Spring的过程中,发现Spring3.0.5版本与Hibernate3.0.5版本不兼容,于是不得不也把Hibernate升级到更高的3.3版本。

2、准备

下载release版本的Spring-3.0.5.jar和Hibernate-3.3.jar包。

http://search.maven.org/

一个强大的jar包下载网站,这里几乎可以找到你所需要的所有jar包及源码

3、更新过程

1) 更新Spring相关的jar

删除原有的Spring jar包,项目原有的jar包为

  1. spring.jar  
  2. spring-beans-2.5.6.jar
  3. spring-context-2.5.6.jar
  4. spring-context-support-2.5.6.jar
  5. spring-core-2.5.6.jar
  6. spring-jdbc-2.5.6.jar
  7. spring-tx-2.5.6.jar
  8. spring-web-2.5.6.jar
  9. spring-webmvc-2.5.6.jar  

增加项目新版的Spring jar文件

  1. org.springframework.aop-3.0.5.RELEASE.jar
  2. org.springframework.asm-3.0.5.RELEASE.jar  
  3. org.springframework.beans-3.0.5.RELEASE.jar  
  4. org.springframework.context-3.0.5.RELEASE.jar  
  5. org.springframework.context.support-3.0.5.RELEASE.jar  
  6. org.springframework.core-3.0.5.RELEASE.jar  
  7. org.springframework.expression-3.0.5.RELEASE.jar
  8. org.springframework.jdbc-3.0.5.RELEASE.jar  
  9. org.springframework.orm-3.0.5.RELEASE.jar   
  10. org.springframework.transaction-3.0.5.RELEASE.jar  
  11. org.springframework.web.servlet-3.0.5.RELEASE.jar
  12. org.springframework.web.struts-3.0.5.RELEASE.jar
  13. org.springframework.web-3.0.5.RELEASE.jar 

另外需要增加三个Spring3.0依赖的jar

 aop的依赖  

  1. aopalliance-1.0.jar 
  2. aspectjweaver-1.5.3.jar

和一个java字节码处理类库

  1. javassist-3.9.0.GA.jar

 

2) 更新web.xml中的Spring启动配置

如果原来这样配置:

Xml代码  

  1. <servlet>  
  2.     <servlet-name>SpringContextServlet</servlet-name>  
  3.     <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  


则修改为:

Xml代码  

  1. <listener>    
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>  

 

3) 更新Spring的所有XML配置文件的头部

原有applicationContext*.xml的头部

Xml代码  

<beans 

    xmlns="http://www.springframework.org/schema/beans" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd 

       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

       http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

       http://www.springframework.org/schema/aop

        >
修改为:

Xml代码  

<?xmlversion="1.0"encoding="UTF-8"?>

<beans 

    xmlns="http://www.springframework.org/schema/beans" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

       http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

       http://www.springframework.org/schema/aop> 


即:将applicationContext*.xml头部中的所有的2.5修改为3.0

4)    修改所有的继承SimpleJdbcDaoSupport的文件

如果项目没有使用,这一步可以忽略。

原代码:

Java代码  

  1. public class AbcDao extends SimpleJdbcDaoSupport {  
  2.    ...(略)  
  3. }  


修改为:

Java代码  

  1. public class AbcDao extends JdbcDaoSupport {  
  2.    ...(略)  
  3. }  

 

注:SimpleJdbcDaoSupport 及SimpleJdbcTemplate,已经在3.1.2版本中被废弃原因是JdbcDaoSupport 或NamedParameterJdbcDaoSupport已经提供了其所有功能,可替换为JdbcDaoSupport 或NamedParameterJdbcDaoSupport(派生于JdbcDaoSupport)
参考地址http://kimsoftware.iteye.com/blog/1554218

5) 升级Hibernate Jar包

经过上面的改造,Spring的升级已经完成,此时可以运行web项目,会发现出现下面的异常:

java.lang.NoClassDefFoundError:org/hibernate/context/CurrentSessionContext

这是因为Hibernate-3.0.5的版本中不包含CurrentSessionContext这个类,因此我们需要把Hibernate升级到3.3(或其他版本,本人仅测试了3.3)

   删除原来的jar:

1.   hibernate3.jar

2.   hibernate-annotations.jar

 

   增加新版本的jar:

1.   hibernate3.jar(包名虽相同,但其实版本不同)

2.   hibernate-annotations.jar

3.   hibernate-commons-annotations.jar

4.   hibernate-entitymanager.jar

5.   hibernate-jpa-2.0-api-1.0.1.Final.jar

 

4、运行&测试

经过上面的处理过程,项目已不再提示错误,运行web项目后,经测试项目的功能基本没有问题,升级完成。

5、总结

项目升级后基本的测试是没有问题的,但不知道有没有其它未知的问题,有待后续的进一步观察和测试

3 0