Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.2.x with Java 8

We are currently using spring 3.2.9. We are thinking of upgrading that to a newer version. When I checked the documentation it says that

Along with 4.0 M1, we’ve released Spring Framework 3.2.3, containing fixes for recently reported issues but also coming with OpenJDK 8 runtime support. Spring Framework 3.2.x will support deployment on JDK 8 runtimes for applications compiled against JDK 7 (with -target 1.7) or earlier.

Does that imply that I can't compile on Java 8?
Should I use spring version 4.0.x if I wanna compile with Java 8?

like image 829
Thusitha Thilina Dayaratne Avatar asked May 06 '15 09:05

Thusitha Thilina Dayaratne


3 Answers

There is a best effort support of JDK8 in the 3.2.x line, as of 3.2.9+.

See SPR-11656 for initial support in 3.2.9 and SPR-11979 for bytecode support improvements in 3.2.10. Please note the support limitations explained in the comments.

For comprehensive support of JDK8, please upgrade to Spring 4.x - there's a dedicated wiki page explaining the upgrade path, and the Spring team made great efforts to make that upgrade experience really easy.

like image 173
Brian Clozel Avatar answered Sep 16 '22 20:09

Brian Clozel


As per my observations, you can actually use spring 3 with code compiled in Java 8, as long as you do not use new java8 syntax in there (like lambdas) in the paths scanned.

So, you can use new APIs (streams ...), but not new syntax (lambdas...).

When I tried, I ended up with startup errors like org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class

like image 33
BiAiB Avatar answered Sep 16 '22 20:09

BiAiB


Versions of the Spring Framework that are older than 4.0M1 do not work with classes that are compiled for Java 8.

Spring tries to Proxy these classes by reading class files, which won't work if they're "too new". If your @Service classes are compiled with Java 8 as the target, Spring will fail to load the classes on startup.

This means that you will have to upgrade to Spring 4.x.x, if you want to target Java 8 (and use lambdas, default implementations and so on).

I encountered this problem myself a few months ago with a project that uses Spring 3.x.x.

like image 38
mhlz Avatar answered Sep 16 '22 20:09

mhlz