简体   繁体   English

与用于配置的XML文件相比,Annotations的实用程序

[英]Utility of Annotations as compared to XML files for configuration

In Java, is it a good practice to use annotations to configure an application rather than using XML files? 在Java中,使用注释来配置应用程序而不是使用XML文件是一种好的做法吗? I am more skeptical about it because, using annotations involves changing the java source files and it is as good as declaring constants in java files and then using those constants, whereas when we make the configurations using XML files, we can keep all the configuration changes away from java source files and keep the configurations in separate XML files, this approach sounds more neat. 我对此更加怀疑,因为使用注释涉及更改java源文件,它与在java文件中声明常量然后使用这些常量一样好,而当我们使用XML文件进行配置时,我们可以保留所有配置更改远离java源文件并将配置保存在单独的XML文件中,这种方法听起来更整洁。 Also, when we need to make changes to configuration, we know which XML file to change rather than searching the java files for the annotations. 此外,当我们需要对配置进行更改时,我们知道要更改哪个XML文件,而不是在java文件中搜索注释。 Also, we can update an XML file in an EAR without compiling the code again, whereas if we make any change in an annotation, then we need to compile the code again. 此外,我们可以更新EAR中的XML文件而无需再次编译代码,而如果我们在注释中进行任何更改,那么我们需要再次编译代码。

Can anybody please throw some light on why should we use annotations and not XML files for configuration? 任何人都可以说明为什么我们应该使用注释而不是XML文件进行配置?

Which to use may vary depending on what's being configured, how the configuration is used, project/cultural conventions, etc. Good IDE support makes using either more convenient and reliable. 使用哪个可能会有所不同,具体取决于配置的内容,配置的使用方式,项目/文化惯例等。良好的IDE支持使得使用更方便和可靠。

Personally, while I use both XML and annotations, I tend to prefer XML configuration for many tasks, particularly on larger projects. 就个人而言,虽然我同时使用XML和注释,但我更倾向于将XML配置用于许多任务,特别是在大型项目中。 For example, with Spring, I prefer XML configuration: it's easier to manage the configuration itself, configuration changesets, and environment-specific changes (eg, testing, server-based, etc.), when it's more localized. 例如,对于Spring,我更喜欢XML配置:当它更加本地化时,更容易管理配置本身,配置更改集和特定于环境的更改(例如,测试,基于服务器等)。

For other configurations, annotations are often more appropriate and convenient. 对于其他配置,注释通常更合适和方便。 For quick projects with little or no domain class customization, Hibernate annotations may be more convenient. 对于很少或没有域类自定义的快速项目,Hibernate注释可能更方便。

Ultimately it's a matter of preference and convenience rather than a purely technical one. 最终,这是一个偏好和便利的问题,而不是纯粹的技术问题。 (Except when the XML and annotations support different features; sometimes one offers more-complete capabilities.) (除非XML和注释支持不同的功能;有时一个提供更完整的功能。)

I prefer annotations since my IDE can help me validate my configurations. 我更喜欢注释,因为我的IDE可以帮助我验证我的配置。 Configurations stored in xml-files cannot be validated before runtime (I'm thinking mostly about spring and injections) 存储在xml文件中的配置在运行之前无法验证(我主要考虑弹簧和注入)

Also, I find that for anything more than a tiny project, a large xml-config is hard to maintain. 此外,我发现除了一个小项目之外,大型xml-config很难维护。

When working with annotations, you have to take care of only one place to configure your stuff (java code). 使用注释时,您只需要处理一个地方来配置您的东西(java代码)。 When configuring with XML, many times a programmer can "forget" to configure a new property or class in the XML, and after the error must correct and restart, resulting in a waste of time. 使用XML进行配置时,程序员很多时候可以“忘记”在XML中配置新属性或类,并且在错误必须纠正并重新启动之后,会导致浪费时间。

I would say it very much depends on what you are configuring. 我会说这在很大程度上取决于你的配置。

If configuration maybe or should be changed after deployment, then in most cases it is preferable to use XML (or other text-based format). 如果在部署之后可能或应该更改配置,则在大多数情况下,最好使用XML(或其他基于文本的格式)。 This would include Hibernate server configuration, Tomcat/Jetty configuration, Log4j configuration, etc. The main advantage is flexibility. 这将包括Hibernate服务器配置,Tomcat / Jetty配置,Log4j配置等。主要优点是灵活性。

For cases when configuration does not need change after deployment, configuration using annotations is preferable. 对于部署后不需要更改配置的情况,最好使用注释进行配置。 Too many configuration files also complicate your software, so it's best to keep it to a minimum. 太多配置文件也会使您的软件复杂化,因此最好将其保持在最低限度。 Good examples would be of annotation-based configurations: Hibernate bean mapping configuration, Spring dependency injection, Guice, etc (some give you both options, but I would prefer annotations here). 好的例子是基于注释的配置:Hibernate bean映射配置,Spring依赖注入,Guice等(有些给你两个选项,但我更喜欢这里的注释)。 The advantage is better manageability, and compile-time checking for errors (this depends on the API, of course). 优点是更好的可管理性,以及错误的编译时检查(当然,这取决于API)。

Personally, when I've tried to understand a new system, having the annotations right there with the code makes it easier to follow and comprehend. 就个人而言,当我试图理解一个新系统时,在那里使用代码注释使得更容易理解和理解。 Hunting for references of the class in configuration files can be a little annoying. 在配置文件中寻找类的引用可能有点烦人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM