spring版本升級到4.2.5踩雷指南
點關注不迷路,歡迎再訪!
精簡博客內容,儘量已行業術語來分享。
努力做到對每一位認可自己的讀者負責。
幫助別人的同時更是豐富自己的良機。
一、新特性介紹
Spring 4 相比 Spring 3,引入許多新特性,這裏列舉幾條較爲重要的:
1.支持 JDK8 (這個是最主要的)。
2.Groovy Bean Definition DSL 風格配置。
3.支持 WebSocket、SockJS、STOMP 消息
4.移除 Deprecated 包和方法
5.一些功能加強,如:核心容器、Web、Test 等等,不一一列舉。
二.引入依賴
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
三.踩雷指南
1.業務項目多數使用的iBATIS
iBATIS 2.x support was removed from Spring in version 4.0 but there is a workaround for that
ibatis (mybatis 的前身)這個 orm 框架,當 spring3 升級 spring4 後,會出現兼容性問題,編譯都不能通過。
因爲 Spring4 官方已經不再支持 ibatis。
解決方案:
添加兼容性jar
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-2-spring</artifactId>
<version>1.0.1</version>
</dependency>
2.security升級問題
建議最後升級spring-security,security3和security4改動較大,spring 4.2.5向下兼容security 3.2.9; ,如有需要更新可升級security至4.2.2。
3.spring.xml問題
spring 不再支持 ref 元素的 local 屬性,如果你的項目中使用了,需要改爲 bean。舉例:
spring4以前
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"> <ref local="dataSource" />
</property>
</bean>
spring4以後
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"> <ref bean="dataSource" />
</property>
</bean>