Spring migration

From 3.x to 4.x

Created by Paolo Escobar / @pao_esco

Agenda

  • Context
  • Migration Strategy
  • Surfing on issues

Context

It's not just about Spring...

  • Spring
  • Hibernate
  • Jersey
  • Ehcache
  • Slf4j
  • And a lot more...

It took one day to migrate from Spring 3.x to Spring IO Platform 1.1.2.RELEASE

Our code is fully covered by automated tests

Migration Strategy

  • Edit pom.xml to add Spring IO Platform
  • Clean pom.xml (remove versions managed by Spring)
  • Fix compilation issues
  • Fix unit testing issues
  • Fix server start issues
  • Fix integration testing issues

Dealing with issues

Compilation

Hibernate.LONG does not exist anymore

Description

The code does not compile

Hibernate 4 removed this attribute

Resolution

Change

query.unwrap(SQLQuery.class).addScalar("id", Hibernate.LONG);

To

query.unwrap(SQLQuery.class).addScalar("id", new LongType());

Compilation

Generics, Mockito and Spring Security : Collection<? extends GrantedAuthority>

Description

Tests don't compile

Changes in Spring Security

Collection<GrantedAuthority> changed to Collection<? extends GrantedAuthority>

Resolution

Change

Mockito.when(auth.getAuthorities()).thenReturn(authorities);

To

Mockito.doReturn(authorities).when(auth).getAuthorities();

Server startup

Another CacheManager with same name 'cacheManager' already exists in the same VM

Description

Hibernate 3 uses ehcache

Hibernate 4 uses it's own internal hibernate-ehcache

Some classes have changed

Resolution

Change

hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory

To

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

Resources

StackOverflow

Server startup

Configuration problem: You cannot use a spring-security-2.0.xsd schema with Spring Security 3.0. Please update your schema declarations to the 3.0 schema."

Description

Spring xml files declare xsd import to use Spring features

XSD versions must be aligned with Spring version in the classpath

Resolution

Change

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"

To

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

Server startup

SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

Description

Spring context is not loading properly on startup

With the upgrade, some old dependencies on servlet-api and jsp-api stayed in the classpath

Resolution

Clean classpath

servlet-api and jsp-api must be provided

Resources

StackOverflow

Server startup

JerseyServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

Description

Spring context is not loading properly on startup

With new versions, configuration has changed

Compatibility between versions (Spring 4 and Jersey 2)

Resolution

In our project : removed Jersey because it was not used

Another solution is to upgrade to Jersey 2.x

Another solution is to migrate to Spring MVC

Database

java.sql.SQLException: data exception: string data, right truncation

Description

Some attributes in @Entity classes are annotated @Lob

Hibernate dialect org.hibernate.dialect.HSQLDialect has changed

It does not handle @Lob the same way

Resolution

Change

@Lob
@Column(name = "BODY" )
public String getBody() {
	return body;
}
								

To

@Lob
@Column(name = "BODY", length = 10000)
public String getBody() {
	return body;
}
								

Resources

StackOverflow