Java Xml 配置与基于注解的配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/182393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:59:56  来源:igfitidea点击:

Xml configuration versus Annotation based configuration

javaxmlspringannotations

提问by abarax

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 或注释)似乎变得越来越重要。随着项目的增长,一致性对于可维护性非常重要。

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 的配置相对于基于注解的配置有什么优势,基于注解的配置相对于基于 XML 的配置有什么优势?

采纳答案by MetroidFan2002

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

注释有其用途,但它们不是杀死 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.

例如,如果使用 Spring,将 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.

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

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.

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

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 表示,因为尽管它最终会影响代码的运行方式,但它不会改变代码的主要功能,因此不属于源文件。

回答by ARKBAN

I might be wrong, but I thought Annotations (as in Java's @Tag and C#'s [Attribute]) were a compile-time option, and XML was a run-time option. That to me says the are not equivalent and have different pros and cons.

我可能错了,但我认为注释(如 Java 的 @Tag 和 C# 的 [Attribute] 中的)是一个编译时选项,而 XML 是一个运行时选项。这对我来说是不等价的,并且有不同的优点和缺点。

回答by Jason

This is the classic 'Configuration versus Convention' question. Personal taste dictates the answer in most cases. However, personally I prefer Configuration (i.e. XML based) over Convention. IMO IDE's are sufficiently robust enough to overcome some of the XML hell people often associate w/ the building and maintaining an XML based approach. In the end, I find the benefits of Configuration (such as building utilities to build, maintain and deploy the XML config file) outweighs Convention in the long run.

这是经典的“配置与约定”问题。在大多数情况下,个人品味决定了答案。然而,我个人更喜欢配置(即基于 XML)而不是约定。IMO IDE 足够健壮,足以克服人们经常与构建和维护基于 XML 的方法相关联的一些 XML 地狱。最后,我发现从长远来看,配置的好处(例如构建实用程序来构建、维护和部署 XML 配置文件)超过约定。

回答by Juraj

It depends on what everything you want to configure, because there are some options that cannot be configured with anotations. If we see it from the side of annotations:

这取决于您要配置的所有内容,因为有些选项无法使用注释进行配置。如果我们从注解的侧面看:

  • plus: annotations are less talky
  • minus: annotations are less visible
  • 加:注释不那么健谈
  • 减号:注释不太明显

It's up to you what is more important...

更重要的是你自己...

In general I would recommend to choose one way and use it all over some closed part of product...

一般来说,我会建议选择一种方式并在产品的某些封闭部分使用它......

(with some exceptions: eg if you choose XML based configurations, it's ok to use @Autowire annotation. It's mixing, but this one helps both readability and maintainability)

(有一些例外:例如,如果您选择基于 XML 的配置,则可以使用 @Autowire 注释。它是混合的,但这有助于可读性和可维护性)

回答by Huibert Gill

I always think about annotations as some kind of indicator of whata class is capable of, or howit interacts with others.

我总是认为注释一个类的能力的某种指标,或者它如何与其他类交互。

Spring XML configuration on the other hand to me is just that, configuration

另一方面,对我来说,Spring XML 配置就是这样,配置

For instance, information about the ip and port of a proxy, is definetly going into an XML file, it is the runtime configuration.

例如,有关代理的 ip 和端口的信息肯定会进入 XML 文件,它是运行时配置。

Using @Autowire,@Elementto indicate the framework what to do with the class is good use of annotations.

使用@Autowire,@Element来指示框架如何处理类是很好的使用注解。

Putting the URL into the @Webserviceannotation is bad style.

将 URL 放入@Webservice注释中是不好的风格。

But this is just my opinion. The line between interaction and configuration is not always clear.

但这只是我的意见。交互和配置之间的界限并不总是很清楚。

回答by Cristian Vat

I also think a mix is the best thing, but it also depends on the type of configuration parameters. I'm working on a Seam project which also uses Spring and I usually deploy it to different development and test servers. So I have split:

我也认为混合是最好的,但这也取决于配置参数的类型。我正在开发一个也使用 Spring 的 Seam 项目,我通常将它部署到不同的开发和测试服务器。所以我分裂了:

  • Server specific configuration (Like absolute paths to resources on server): Spring XML file
  • Injecting beans as members of other beans (or reusing a Spring XML defined value in many beans): Annotations
  • 服务器特定配置(如服务器上资源的绝对路径):Spring XML 文件
  • 将 bean 作为其他 bean 的成员注入(或在许多 bean 中重用 Spring XML 定义的值):注释

The key difference is that you don't have to recompile the code for all changing server-specific configurations, just edit the xml file. There's also the advantage that some configuration changes can be done by team members who don't understand all the code involved.

关键区别在于您不必为所有更改的特定于服务器的配置重新编译代码,只需编辑 xml 文件。还有一个好处是,一些配置更改可以由不了解所有相关代码的团队成员完成。

回答by Chochos

I use both. Mostly XML, but when I have a bunch of beans that inherit from a common class and have common properties, I use annotations for those, in the superclass, so I don't have to set the same properties for each bean. Because I'm a bit of a control freak, I use @Resource(name="referredBean") instead of just autowiring stuff (and save myself a lot of trouble if I ever need another bean of the same class as the original referredBean).

我两个都用。主要是 XML,但是当我有一堆从公共类继承并具有公共属性的 bean 时,我在超类中为这些 bean 使用注释,因此我不必为每个 bean 设置相同的属性。因为我有点控制狂,所以我使用 @Resource(name="referredBean") 而不是自动装配的东西(如果我需要另一个与原始referredBean 相同类的bean,我会省去很多麻烦) .

回答by krosenvold

An important part in using an annotation-only approach is that the concept of a "bean name" more or less goes away (becomes insignificant).

使用仅注释方法的一个重要部分是“bean 名称”的概念或多或少会消失(变得无关紧要)。

The "bean names" in Spring form an additional level of abstraction over the implementing classes. With XML beans are defined and referenced relative to their bean name. With annotations they are referenced by their class/interface. (Although the bean name exists, you do not need to know it)

Spring 中的“bean 名称”形成了对实现类的额外抽象级别。XML bean 是相对于它们的 bean 名称定义和引用的。通过注释,它们被它们的类/接口引用。(虽然bean名称存在,但你不需要知道)

I strongly believe that getting rid of superfluous abstractions simplifies systems and improves productivity. For largeprojects I think the gains by getting rid of XML can be substantial.

我坚信摆脱多余的抽象可以简化系统并提高生产力。对于大型项目,我认为摆脱 XML 的收益是巨大的。

回答by cliff.meyers

I've been using Spring for a few years now and the amount of XML that was required was definitely getting tedious. Between the new XML schemas and annotation support in Spring 2.5 I usually do these things:

我已经使用 Spring 几年了,所需的 XML 数量肯定变得乏味了。在 Spring 2.5 中的新 XML 模式和注释支持之间,我通常会做这些事情:

  1. Using "component-scan" to autoload classes which use @Repository, @Service or @Component. I usually give every bean a name and then wire them together using @Resource. I find that this plumbing doesn't change very often so annotations make sense.

  2. Using the "aop" namespace for all AOP. This really works great. I still use it for transactions too because putting @Transactional all over the place is kind of a drag. You can create named pointcuts for methods on any service or repository and very quickly apply the advice.

  3. I use LocalContainerEntityManagerFactoryBean along with HibernateJpaVendorAdapter to configure Hibernate. This lets Hibernate easily auto-discover @Entity classes on the classpath. Then I create a named SessionFactory bean using "factory-bean" and "factory-method" referring to the LCEMFB.

  1. 使用“组件扫描”自动加载使用@Repository、@Service 或@Component 的类。我通常给每个 bean 一个名称,然后使用 @Resource 将它们连接在一起。我发现这个管道不会经常改变,所以注释是有意义的。

  2. 对所有 AOP 使用“aop”命名空间。这真的很好用。我仍然将它用于交易,因为将 @Transactional 放在所有地方是一种拖累。您可以为任何服务或存储库上的方法创建命名切入点,并非常快速地应用建议。

  3. 我使用 LocalContainerEntityManagerFactoryBean 和 HibernateJpaVendorAdapter 来配置 Hibernate。这让 Hibernate 可以轻松地自动发现类路径上的 @Entity 类。然后我使用引用 LCEMFB 的“factory-bean”和“factory-method”创建一个命名的 SessionFactory bean。

回答by Charles Chen

I think that visibility is a big win with an XML based approach. I find that the XML isn't really that bad, given the various tools out there for navigating XML documents (i.e. Visual Studio + ReSharper's File Structure window).

我认为可见性是基于 XML 的方法的一大胜利。我发现 XML 并不是那么糟糕,因为有各种用于导航 XML 文档的工具(即 Visual Studio + ReSharper 的文件结构窗口)。

You can certainly take a mixed approach, but that seems dangerous to me if only because, potentially, it would make it difficult for new developers on a project to figure out where different objects are configured or mapped.

您当然可以采用混合方法,但这对我来说似乎很危险,因为这可能会使项目中的新开发人员难以确定不同对象的配置或映射位置。

I don't know; in the end XML Hell doesn't seem all that bad to me.

我不知道; 最后,XML Hell 对我来说似乎并没有那么糟糕。