与用于配置的 XML 文件相比,注释的实用性
在 Java 中,使用注释来配置应用程序而不是使用 XML 文件是一个好习惯吗?我对此比较怀疑,因为使用注释涉及更改java源文件,它与在java文件中声明常量然后使用这些常量一样好,而当我们使用XML文件进行配置时,我们可以保留所有配置更改远离 java 源文件并将配置保存在单独的 XML 文件中,这种方法听起来更简洁。此外,当我们需要更改配置时,我们知道要更改哪个 XML 文件,而不是在 java 文件中搜索注释。此外,我们可以更新 EAR 中的 XML 文件,而无需再次编译代码,而如果我们对注释进行任何更改,则需要再次编译代码。
有人可以解释一下为什么我们应该使用注释而不是 XML 文件进行配置吗?
In Java, is it a good practice to use annotations to configure an application rather than using XML files? 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. 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. 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.
Can anybody please throw some light on why should we use annotations and not XML files for configuration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用哪一个可能会有所不同,具体取决于配置的内容、配置的使用方式、项目/文化约定等。良好的 IDE 支持使使用更加方便和可靠。
就我个人而言,虽然我同时使用 XML 和注释,但对于许多任务,尤其是大型项目,我更倾向于使用 XML 配置。例如,对于 Spring,我更喜欢 XML 配置:当它更加本地化时,更容易管理配置本身、配置更改集和特定于环境的更改(例如,测试、基于服务器等)。
对于其他配置,注释往往更合适、更方便。对于很少或没有域类自定义的快速项目,Hibernate 注释可能更方便。
归根结底,这是一个偏好和便利性问题,而不是纯粹的技术问题。 (除非 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.
Personally, while I use both XML and annotations, I tend to prefer XML configuration for many tasks, particularly on larger projects. For example, with Spring, I prefer XML configuration: it's easier to manage the configuration itself, configuration changesets, and environment-specific changes (e.g., testing, server-based, etc.), when it's more localized.
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.
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.)
我更喜欢注释,因为我的 IDE 可以帮助我验证我的配置。存储在 xml 文件中的配置无法在运行前进行验证(我主要考虑的是 spring 和注入)
此外,我发现对于超过一个小项目的任何东西,大型 xml 配置都很难维护。
I prefer annotations since my IDE can help me validate my configurations. Configurations stored in xml-files cannot be validated before runtime (I'm thinking mostly about spring and injections)
Also, I find that for anything more than a tiny project, a large xml-config is hard to maintain.
使用注释时,您只需处理一个地方来配置您的东西(java 代码)。当使用XML进行配置时,很多时候程序员可能会“忘记”在XML中配置新的属性或类,并且在错误之后必须纠正并重新启动,从而导致时间的浪费。
When working with annotations, you have to take care of only one place to configure your stuff (java code). 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(或其他基于文本的格式)。这将包括Hibernate服务器配置、Tomcat/Jetty配置、Log4j配置等。主要优点是灵活性。
对于部署后不需要更改配置的情况,最好使用注释进行配置。太多的配置文件也会使您的软件变得复杂,因此最好将其保持在最低限度。很好的例子是基于注释的配置:Hibernate bean 映射配置、Spring 依赖注入、Guice 等(有些为您提供这两个选项,但我更喜欢这里的注释)。优点是更好的可管理性,以及编译时错误检查(当然,这取决于 API)。
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). This would include Hibernate server configuration, Tomcat/Jetty configuration, Log4j configuration, etc. The main advantage is flexibility.
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). The advantage is better manageability, and compile-time checking for errors (this depends on the API, of course).
就我个人而言,当我尝试理解一个新系统时,将注释与代码放在一起可以更容易理解和理解。在配置文件中寻找类的引用可能有点烦人。
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.