简体   繁体   中英

Dependency on Spring's annotations

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

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.

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 (ie only the fields) for use in the other environment.

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:

 
 
 
 
  
  
  public class MyDAO { protected SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } // .. Your DAO code resides here .. }
 
 
  

And the Spring sub-class:

 
 
 
 
  
  
  @Repository public class MySpringDAO extends MyDAO { @AutoWired protected SessionFactory sessionFactory; }
 
 
  

This way, your non-Spring project can use "MyDAO", and exclude the "MySpringDAO" class from the build.

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. Ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM