`
cgs1999
  • 浏览: 530366 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

升级Spring从2.5.6至3.1.2过程实录

阅读更多
1、引言
项目使用的是Spring MVC + Spring + iBatis框架,Spring和Spring MVC的版本都是2.5.6,但项目功能开发上需要支持REST功能,Spring MVC 2.5.6对REST的支持不够,于是决定升级Spring及Spring MVC至最新版本3.1.2

2、准备
至SpringSource的官方网站下载最新的Spring版本(当前最新release版本为3.1.2),地址
http://www.springsource.org/download/community

3、更新过程
(1)更新Spring和Spring MVC相关的jar
删除原有的Spring依赖,项目原有的依赖为
spring-2.5.6.jar
spring-webmvc-2.5.6.jar
spring-test-2.5.6.jar

增加项目新版的Spring依赖文件
// aop的依赖
aopalliance-1.0.jar
// 增加spring3.1.2相关jar
org.springframework.beans-3.1.2.RELEASE.jar
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.context.support-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.jdbc-3.1.2.RELEASE.jar
org.springframework.orm-3.1.2.RELEASE.jar
org.springframework.test-3.1.2.RELEASE.jar
org.springframework.transaction-3.1.2.RELEASE.jar
org.springframework.web-3.1.2.RELEASE.jar
org.springframework.web.servlet-3.1.2.RELEASE.jar


(2)更新web.xml中的Spring启动配置
原内容:
<servlet>
	<servlet-name>SpringContextServlet</servlet-name>
	<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

修改为:
<listener>  
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


(3)更新Spring的所有XML配置文件的头部
原有applicationContext*.xml的头部
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

修改为:
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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">

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

(4)更新Spring MVC的所有XML配置文件的头部
原有servlet*.xml的头部
 <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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans   
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/context   
     http://www.springframework.org/schema/context/spring-context-2.5.xsd">

修改为:
 <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: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">

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

(5)修改所有的继承SimpleJdbcDaoSupport的文件
原代码:
public class AAASDao extends SimpleJdbcDaoSupport {
   ...(略)
}

修改为:
public class AAASDao extends JdbcDaoSupport {
   ...(略)
}

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

(6)另外,Spring MVC基于注解的Junit测试的注解
原内容
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class BaseTest extends AbstractJUnit4SpringContextTests {
   ...(略)
}

@RunWith(SpringJUnit4ClassRunner.class)会报错,网上找了一下,不知如何处理,后来由于该部分的代码,项目中已经没有用到了,故将其删掉了,有兴趣的童靴可以再找找修正方法

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

5、总结
项目升级后基本的测试是没有问题的,但不知道有没有其它未知的问题,有待后续的进一步观察和测试
分享到:
评论
1 楼 looxiaohu 2014-07-04  
你好,我现在有个restdemo,spring3.1.2 jersey1.8 hibernate4.17 注解方式
运行报空指针错误严重: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.hondent.resource.PaymentResource.savePayment(PaymentResource.java:37)
-------------------------------------------------------------------------------
@Repository("userDao")
public class UserDaoImpl implements UserDaoI
{
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
--------------------------------------------------------------------------------@Service("userService")
public class UserServiceImpl implements UserServiceI
{
    private UserDaoI userDao;

    public UserDaoI getUserDao()
    {
        return userDao;
    }

    @Autowired
    public void setUserDao(UserDaoI userDao)
    {
        this.userDao = userDao;
    }
-------------------------------------------------------------------------------
@Path("/payment")
public class PaymentResource
{
    private UserServiceI userService;

    public UserServiceI getUserService()
    {
        return userService;
    }

    @Autowired
    public void setUserService(UserServiceI userService)
    {
        this.userService = userService;
    }

    @GET
    @Path("/savePayment")
    public Response savePayment()
    {
        Tuser tuser = new Tuser();
        tuser.setId(UUID.randomUUID().toString());
        tuser.setName("xxxx");
        tuser.setPwd("123456xyy");
        String result = userService.save(tuser);空指针
楼主有没有遇到过这种情况?


相关推荐

Global site tag (gtag.js) - Google Analytics