Xml 配置与基于注解的配置 [英] Xml configuration versus Annotation based configuration

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

问题描述

在我最近从事的一些大型项目中,选择其中一个(XML 或注释)似乎变得越来越重要.随着项目的增长,一致性对于可维护性非常重要.

In a few large projects i have been working on lately it seems to become increasingly important to choose one or the other (XML or Annotation). As projects grow, consistency is very important for maintainability.

我的问题是:基于 XML 的配置与基于注解的配置相比有哪些优势?基于注解​​的配置与基于 XML 的配置相比有哪些优势?

My questions are: what are the advantages of XML-based configuration over Annotation-based configuration and what are the advantages of Annotation-based configuration over XML-based configuration?

推荐答案

注解有它们的用途,但它们并不是杀死 XML 配置的灵丹妙药.我建议将两者混合使用!

Annotations have their use, but they are not the one silver bullet to kill XML configuration. I recommend mixing the two!

例如,如果使用 Spring,将 XML 用于应用程序的依赖注入部分是完全直观的.这使代码的依赖项远离将使用它的代码,相比之下,在需要依赖项的代码中使用某种注释使代码知道这种自动配置.

For instance, if using Spring, it is entirely intuitive to use XML for the dependency injection portion of your application. This gets the code's dependencies away from the code which will be using it, by contrast, using some sort of annotation in the code that needs the dependencies makes the code aware of this automatic configuration.

然而,不是使用 XML 进行事务管理,而是使用注释将方法标记为事务性是非常有意义的,因为这是程序员可能希望知道的信息.但是一个接口将作为 SubtypeY 而不是 SubtypeX 被注入到类中不应该包含在类中,因为如果现在你想注入 SubtypeX,你必须改变你的代码,而你之前有一个接口契约,所以使用 XML,您只需要更改 XML 映射即可,而且这样做相当快速且轻松.

However, instead of using XML for transactional management, marking a method as transactional with an annotation makes perfect sense, since this is information a programmer would probably wish to know. But that an interface is going to be injected as a SubtypeY instead of a SubtypeX should not be included in the class, because if now you wish to inject SubtypeX, you have to change your code, whereas you had an interface contract before anyways, so with XML, you would just need to change the XML mappings and it is fairly quick and painless to do so.

我没有使用过 JPA 注释,所以我不知道它们有多好,但我认为将 bean 映射到 XML 中的数据库也很好,因为对象不应该关心它的位置信息来自,它应该只关心它可以用它的信息做什么.但如果你喜欢 JPA(我没有任何经验),一定要试试.

I haven't used JPA annotations, so I don't know how good they are, but I would argue that leaving the mapping of beans to the database in XML is also good, as the object shouldn't care where its information came from, it should just care what it can do with its information. But if you like JPA (I don't have any expirience with it), by all means, go for it.

一般来说:如果一个注解提供了功能并且本身就充当了一个注释,并且没有将代码绑定到某个特定的过程以便在没有这个注解的情况下正常运行,那么去使用注解.例如,标记为事务性的事务性方法不会终止其操作逻辑,并且还可以作为良好的代码级注释.否则,此信息可能最好用 XML 表示,因为尽管它最终会影响代码的运行方式,但它不会改变代码的主要功能,因此不属于源文件.

In general: If an annotation provides functionality and acts as a comment in and of itself, and doesn't tie the code down to some specific process in order to function normally without this annotation, then go for annotations. For example, a transactional method marked as being transactional does not kill its operating logic, and serves as a good code-level comment as well. Otherwise, this information is probably best expressed as XML, because although it will eventually affect how the code operates, it won't change the main functionality of the code, and hence doesn't belong in the source files.

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

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