简体   繁体   English

对Spring注释的依赖

[英]Dependency on Spring's annotations

I have annotated my classes with @Repository, @Resource, @Component, @Service annotations but these classes must run in 2 environments. 我已经用@ Repository,@ Resource,@ Component,@ Service批注对类进行了批注,但是这些类必须在2种环境中运行。 The first environment is Spring 2.x based while the other has no spring at all. 第一个环境是基于Spring 2.x的,而另一个环境根本没有Spring。 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 我敢肯定,如果没有spring jars,代码将失败,并且我想从您那里了解如何保留注释,但仍然可以在两种环境下工作的想法

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. 为了能够使用您提到的注释,或者说真正地让Spring为您使用注释,以便从中获得好处,您至少需要使用Spring 2.5.x,这是它们被引入的时间。

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. 因为当您使用spring 2.0时,不会有代码尝试“扫描”它们。

For every annotation in Java, there is a corresponding class file. 对于Java中的每个注释,都有一个对应的类文件。 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. 如果该类还具有方法,则可以重新编译注释源(使用相同的序列化ID),而无需使用方法(即仅字段)来在其他环境中使用。

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 由于您无法删除已接受的帖子,因此我建议您阅读/投票给Hans的帖子,这比我原来的帖子要好得多: 依赖于Spring的注释


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. 当使用构造型注释(@Service等)时,获得编译时bean验证的权衡是您已耦合到代码中的spring-context库。 I see 3 immediate options: 我看到3个立即选择:

1) Remove the annotations, and configure your beans in XML. 1)删除注释,并以XML配置您的bean。

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. 2)将spring-context.jar(或包含构造型批注的等效库)复制到非Spring项目中以解决依赖关系,但是将Spring保留为未配置状态,以便在您的代码中不使用它。

3) Remove the annotations from your concrete classes, and extend them with "Spring" versions. 3)从具体的类中删除注释,并使用“ Spring”版本进行扩展。 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: 和Spring子类:

 
 
 
 
  
  
  @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. 这样,您的非Spring项目可以使用“ MyDAO”,并从构建中排除“ MySpringDAO”类。

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. 即定义com.yourcompany .... Component等。

I assume that you use a <context:component-scan .../> somewhere in your spring config. 我假设您在spring配置中的某个地方使用了<context:component-scan .../> Just add use-default-filters="false" and define your own filter to match your annotations. 只需添加use-default-filters="false"并定义您自己的过滤器以匹配您的注释即可。

Look for the PostProcessors that actually do the grunt work. 寻找实际完成艰苦工作的PostProcessor。 They can be configured to use an alternate set of annotations. 可以将它们配置为使用备用注释集。 @Repository is examined by PersistenceExceptionTranslationPostProcessor . @RepositoryPersistenceExceptionTranslationPostProcessor检查。 This PostProcessor can be configured to use your equivalent to the annotation. 可以将此PostProcessor配置为使用与注解等效的PostProcessor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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