I have annotated my classes with @Repository, @Resource, @Component, @Service annotations but these classes must run in 2 environments. The first environment is Spring 2.x based while the other has no spring at all. I’m sure the code will fail without the spring jars & I want to know ideas from you on how I can retain the annotations but still work in both environments
I have annotated my classes with @Repository, @Resource, @Component, @Service annotations but these classes
Share
Since you cannot delete an accepted post, I suggest you read/vote Hans’ post, which is a much better explanation than my original one: Dependency on Spring's annotations
When using the stereotype annotations (@Service etc), the trade-off for gaining the compile-time bean validation is that you become coupled to the spring-context library in your code. I see 3 immediate options:1) Remove the annotations, and configure your beans in XML.
2) Copy the spring-context.jar (or the equivalent library holding your stereotype annotations) into your non-Spring project to resolve the dependencies, but leave Spring unconfigured so it is not used in your code.
3) Remove the annotations from your concrete classes, and extend them with “Spring” versions. This approach may or may not be a little invasive to your design, but worth considering:
And the Spring sub-class:
This way, your non-Spring project can use “MyDAO”, and exclude the “MySpringDAO” class from the build.