問題描述
在我最近從事的一些大型項目中,選擇其中一個(XML 或 Annotation)似乎變得越來越重要.隨著項目的增長,一致性對于可維護性非常重要.
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 配置與基于注釋的配置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!