Home > Back-end >  Is it possible to write lib for projects using different versions of Spring?
Is it possible to write lib for projects using different versions of Spring?

Time:12-27

I'm writing a lib for projects using different versions of Spring. The lib itself is based on Spring too ( more precisely, Spring Cloud Sleuth). For now, I use different versions for different projects( version1 for projects using Spring boot 2.0.x, version2 for projects using Spring boot 2.3.x, etc). Apparently, the maintenance took a lot of time and made some confusion. Is there a runtime mechanism like @Conditional but for dependencies?

CodePudding user response:

First, check the Spring Cloud compatibility matrix. As you can see, different Spring Cloud versions support different Boot versions.

I would do the same for your library and maintain different versions of it. Your can have optional dependencies on Sleuth and set things up using @Conditional annotations (e.g.: @ConditionalOnClass) but I would not recommend that.

Sleuth 2.2.x (Hoxton) uses Brave's API (btw 2.x is not supported anymore, you should upgrade). Sleuth 3.0.x (2020.0.x aka Ilford) and 3.1.x (2021.0.x aka Jubilee) have their own API and they abstract the tracer libraries away. You can use these interfaces/classes to detect the version and configure them differently but when you compile your library you can have classpath issues because you have 2.2.x, 3.0.x, and 3.1.x on your classpath.

Another thing you can do is modularize your library and put all of those things that does not depend on Spring into a "core" module then create smaller adapter/autoconfiguration/starter modules for every version of Spring Cloud you want to support.

  • Related