spring3.X -----> spring 4.X

一、首先我们来罗列一下Spring4.x的大变化。

  1. 支持JDK8 (这个是最主要的)。
  2. Groovy Bean Definition DSL 风格配置。

  3. 依赖注入支持泛型。

  4. 合并部分注解,如@RestController   @Controller + @ReponseBody  。

  5. Spring-test 部分功能的改进。

不扯了。言归正传。升级要注意什么。

二、把Spring 相关Jar包换成4.x的jar包。

基本上你把原来的Spring 3.x 的包,直接替换成你需要的Spring 4.x 的包。如我就是从Spring 3.2.0.RELEASE  ==> Spring 4.2.5.RELEASE  升级,如果包有问题,你直接用下面的(一般项目基本够了)。


  
  
  1. <!-- Spring 在 3.2.13版本后,要单独引用 -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>4.2.5.RELEASE</version>
  6. </dependency>
  7. <!-- Spring 在 3.2.13版本后,要单独引用 -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-test</artifactId>
  11. <version>4.2.5.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-webmvc</artifactId>
  16. <version>4.2.5.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-context-support</artifactId>
  21. <version>4.2.5.RELEASE</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-core</artifactId>
  26. <version>4.2.5.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-jdbc</artifactId>
  31. <version>4.2.5.RELEASE</version>
  32. </dependency>

好了,差不多了。这里需要注意的是spring-context 包,因为我发现在Spring 3.2.13 后就要单独引用,因为有的.class 从原来其他的包中拆出来了。如定时任务(我不能肯定)。

三、改变Spring各种配置文件。


  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
  13. default-lazy-init="false">

之前各个 Spring  的配置文件,应该是这样的,其他不用动,把3.0 改成4.0 即可,或者把-3.0 直接删除也可以。建议配置为4.0 

PS:这里有个小细节。

如果你用到了ref 标签的 bean 。如:


  
  
  1. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  2. .....
  3. <property name="sessionListeners">
  4. <list>
  5. <ref local="customSessionListener"/>
  6. </list>
  7. </property>
  8. .....
  9. </bean>

这里的ref 中, Spring  4.0 后不能用local ,你得改成bean 。也就是改成这样。<ref bean="customSessionListener"/> 


  
  
  1. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  2. .....
  3. <property name="sessionListeners">
  4. <list>
  5. <ref bean="customSessionListener"/>
  6. </list>
  7. </property>
  8. .....
  9. </bean>

如果不改启动报错:cvc-complex-type.3.2.2: 元素 'ref' 中不允许出现属性 'local' 。

这样就基本完事了。

启动项目吧。可能没什么错误。你访问个 Ajax  链接试试。

发现报错如下:


  
  
  1. org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.JavaType.isReferenceType()Z
  2. 省略200
  3. ....

那么把下面 Spring  4.x 依赖的相关 json   jar  包加上即可。


  
  
  1. <!-- Spring 升级4+ 依赖的JSON包 -->
  2. <dependency>
  3. <groupId>com.fasterxml.jackson.core</groupId>
  4. <artifactId>jackson-databind</artifactId>
  5. <version>2.7.4</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.fasterxml.jackson.core</groupId>
  9. <artifactId>jackson-core</artifactId>
  10. <version>2.7.4</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.fasterxml.jackson.core</groupId>
  14. <artifactId>jackson-annotations</artifactId>
  15. <version>2.7.4</version>
  16. </dependency>
  17. <!-- /Spring 升级4+ 依赖的JSON包 -->

如果还是出现错误,请检查 Spring  的jar是不是4.5或者以下。或者把 jackson  包降低版本。基本能解决!

好,打完收工。如果还有其他错误,请加群问我,知无不答。

版权所属:SO JSON在线解析

原文地址:http://www.sojson.com/blog/145.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值