春天注解基于DI VS xml配置? [英] Spring annotation-based DI vs xml configuration?

查看:171
本文介绍了春天注解基于DI VS xml配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,在我们的团队,我们开始使用Spring注解在code定义Spring的依赖讨论。目前我们使用的context.xml来定义我们的依赖。你能给我为这两种方法的一些线索,而当一个是更好地用于?

Recently in our team we started discussing using spring annotations in code to define spring dependencies. Currently we are using context.xml to define our dependencies. Would you give me some clues for either approach, and when one is better to be used?

编辑:我知道,这似乎是一个更一般的一个重复的问题,但我感兴趣的注释的影响VS的配置只依赖注入,我相信会比一般的问题不同的回答和态度

I know this seems a duplicate question to a more-general one, but I am interested in the impacts of annotations vs configuration for dependency injection only, which I believe would have different answers and attitude than the general question.

推荐答案

在队中,我们得出以下结论这里阅读一些相关的职位,并具有进一步讨论后。我希望将别人有用这里。

After reading some related posts here and having further discussion in the team we come to the following conclusions. I hope the would be useful to others here.

关于XML配置(我们使用的是到现在为止),我们决定保留它是为图书馆的依赖(或者被我们开发,或由第三方)。根据定义库应该只提供某些功能,并且可以在各种情况下,这可能不是总是涉及DI被使用。因此,使用说明我们的库项目将带来DI框架(春季在我们的例子)的依赖到库,而当库不使用DI,这将是无用的。有额外的依赖并不认为我们的团队中一个很好的做法(一般恕我直言的)。当我们组装应用程序,应用程序所需要的DI依赖和库类的注入,配置完成。
XML也有利于提供mock实现许多组件,无需重新编译,将使用它们的模块。在测试或在本地或生产环境中运行时,这为我们提供了灵活性。

About XML configuration (which we are using up to now), we decided to keep it for dependencies that are for libraries (being either developed by us, or by third parties). Libraries by definition should provide certain functionality only and can be used in various scenarios, which may not always involve DI. Therefore using annotations in our library projects will bring dependencies of the DI framework (Spring in our case) to the library, and this would be useless when the library is used without DI. Having extra dependencies is not considered a good practice among our team (an in general IMHO). When we are assembling an application, the application has the needed DI dependencies and the injection of library classes is done by configuration. XML is also good for providing mock implementations for many components, without recompiling the modules that will use them. This gives us flexibility when testing or running in local or production environment.

关于注释,我们实际上决定了可以使用它们当注入的组件将不会改变中受益,T.E.不会有场景中的部件的不同的实现将被使用。我们决定,注解将是有明确的功能小模块/应用程序,不会改变或支持不同的实现非常有用的。这些模块我们几乎可以用出不同项目的方块,无需编写繁琐的XML。然而,我们认为,这些模块应该有我们的技术文件中所描述的依赖关系,以便组装整个应用程序时,可以有这些依赖关系的想法不通过code滚动,甚至加载模块在IDE 。仍然,似乎注释将需要该模块为约什么依赖它将使用有所成熟。这也适用于java类的配置,这是XML上下文的现代弹簧替代。

About annotations, we actually decided that we can benefit using them when the injected components will not vary, t.e. there will not be scenarios where different implementations of a component would be used. We decided that annotations will be very useful for small modules/applications with clear functionality, that will not change or support different implementations. Such modules we can almost use out of the box in different projects, without writing the tedious XML. However, we agreed that these modules should have the dependencies described in our technical documentation, so that when assembling the entire application, one can have an idea of these dependencies without scrolling through the code, or even loading the module in the IDE. Still, it seems that annotations will require the module to be somewhat mature about what dependencies it will use. This is also applicable for the java class configurations, which is the modern Spring alternative of XML contexts.

在一般来说,如果我们的依赖性可能会为不同的场景变化,或模块可以与不同的组件使用,我们决定保留XML,而不是硬code这样的模块中的任何依赖关系。显然,必须为用法两种方法之间的右键的平衡,一个清晰的思路。

In general if our dependencies may vary for different scenarios, or a module can be used with different components, we decided to keep the XML and not to hard-code any dependencies in such module. Clearly, there MUST be a right balance between both approaches, and a clear idea for the usages.

有关混合方式的一个重要的更新。最近,我们曾与我们为我们的QA团队,这需要依赖从其他项目中创建了一个测试框架的情况。该框架被设计为使用注解方法和Spring配置类,而引用的项目有,我们需要参考一些XML上下文。不幸的是,测试类(我们使用org.testng具有弹性支承)只能用XML或Java配置类的工作,而不是两个。

An important update regarding the mixed approach. Recently we had a case with a test framework we created for our QA team, which required dependencies from another project. The framework was designed to use the annotation approach and Spring configuration classes, while the referenced project has some xml contexts that we needed to reference. Unfortunately, the test classes (we used org.testng with spring support) could only work with either xml or java configuration classes, not both.

这情况说明,其中这两种方法会发生冲突,并明确,必须被丢弃的情况。在我们的例子中,我们迁移的测试框架使用Spring XML上下文,但其他用途可能暗示周围的其他方式。

This situation illustrates a case where both approaches would clash and clearly, one must be discarded. In our case, we migrated the test framework to use spring xml contexts, but other uses could imply the other way around.

这篇关于春天注解基于DI VS xml配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆