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
For every annotation in Java, there is a corresponding class file. If you find out which annotations you use, you can copy the class files over to the other environment.
I'm not sure whether these classes are dependent on other classes aswel; they probably are not, since annotations are immutable data-only objects. If the class also has methods, you can re-compile (with the same Serialization ID) the annotation sources without the methods (i.e. only the fields) for use in the other environment.
To be able to use the annotations that you mention, or really, let Spring use them for you so you get the benefit, you need to use at least Spring 2.5.x, that's when they were introduced.
Furthermore, annotations are not required to be on the classpath. They will just be ignored. Since when you are using spring 2.0 there will be no code that tries to 'scan' for them.
It wouldn't be spring if it forced you to make your classes directly dependent on it.
You can define your own annotations that serve the same purpose as the spring proved ones. I.e. define com.yourcompany....Component etc.
I assume that you use a <context:component-scan .../>
somewhere in your spring config.
Just add use-default-filters="false"
and define your own filter to match your annotations.
Look for the PostProcessors that actually do the grunt work. They can be configured to use an alternate set of annotations. @Repository
is examined by PersistenceExceptionTranslationPostProcessor
. This PostProcessor can be configured to use your equivalent to the annotation.