Spring Annotation Dependence

I annotated my classes with @Repository, @Resource, @Component, @Service annotations, but these classes should work in two environments. The first environment is Spring 2.x, while the other does not have Spring at all. I am sure that the code will work without Spring jars, and I want to learn from you ideas on how to save annotations, but still work in both environments

+5
java spring annotations
Mar 18
source share
4 answers

Since you cannot delete the received message, I suggest you read / vote for the Hans message, this is a much better explanation than my original: Spring Annotation Dependence




When using stereotype annotations (@Service, etc.), the trade-off for getting a companion bean check is that you join the spring-context library in your code. I see 3 immediate options:

1) Remove annotations and configure beans in XML.

2) Copy spring -context.jar (or the equivalent library with your stereotype annotations) into a non-Spring project to resolve dependencies, but leave Spring unconfigured so that it is not used in your code.

3) Remove annotations from your specific classes and extend them with Spring versions. This approach may or may not be a bit invasive for your design, but you should consider:

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

And subclass of Spring:

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

Thus, your Spring project can use "MyDAO" and exclude the "MySpringDAO" class from the assembly.

0
Mar 18
source share

To use the annotations 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 when they were introduced.

In addition, annotations should not be in the classpath. They will simply be ignored. Because when using Spring 2.0 there will be no code that tries to "scan" for them.

+5
Mar 18
source share

For each annotation in Java, there is a corresponding class file. If you find out which annotations you are using, you can copy the class files to another environment.

I'm not sure if these classes depend on other aswel classes; they probably aren't, because annotations are immutable objects for data only. If the class also has methods, you can recompile (with the same Serialization identifier) ​​annotation sources without methods (i.e. fields only) for use in another environment.

+1
Mar 18 '10 at 8:45
source share

It would not be spring if it made you make your classes directly depend on it.

You can define your own annotations that serve the same purpose as spring. That is, define com.yourcompany .... Component, etc.

I assume that you are using <context:component-scan .../> somewhere in your spring config. Just add use-default-filters="false" and define your own filter according to the annotations.

Look at PostProcessors that actually do grunt work. They can be configured to use an alternative set of annotations. @Repository considered by PersistenceExceptionTranslationPostProcessor . This PostProcessor can be customized to use your equivalent annotation.

0
Mar 20 '10 at 15:41
source share



All Articles