Spring 3.2.x with Java 8

18,090

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
18,090
Thusitha Thilina Dayaratne
Author by

Thusitha Thilina Dayaratne

IT graduate who seek for new challenges. Hobbies - Reading, Programming for fun, Watching TV series

Updated on June 17, 2022

Comments

  • Thusitha Thilina Dayaratne
    Thusitha Thilina Dayaratne almost 2 years

    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?