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

查看:278
本文介绍了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.

我的问题是,什么人preFER。你preFER XML或基于注释?或两者?每个人都在谈论XML配置地狱和注释如何回答,关于注解的配置到底是什么?

My question is, what do people prefer. Do you prefer XML based or Annotation based? or Both? Everybody talks about XML configuration hell and how annotations are the answer, what about Annotation configuration hell?

推荐答案

注解有其用途,但他们不是一个银弹杀死XML配置。我建议混合两种!

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

例如,如果使用Spring,完全直观的使用XML的应用程序的依赖注入部分。这得到了code的依赖关系将会使用它,相比之下,code而去,使用某种在code,需要依赖注解使得code知道这个自动配置

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,你必须改变你的code,而在你面前有一个接口协议反正,所以用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注解,所以我不知道他们是怎么好,但我认为离开豆映射到XML数据库也不错,因为对象不应该关心它在哪里信息来自,它应该只关心什么可以用它做的信息。但是,如果你喜欢JPA(我没有任何与它的expirience),通过各种手段,去了。

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.

在一般:
如果一个注解所提供的功能,并充当其本身和注释,不扳平code到一些具体的过程中,以没有这个注释正常工作,然后去注解。例如,一个事务性方法标记为事务性不杀它的操作逻辑,并作为一个很好的code级评价为好。否则,该信息可能是最好的前pressed如XML,因为尽管它最终将影响如何code工作时,它不会改变code的主要功能,因此不属于在源文件中。

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天全站免登陆