当我查看 Hibernate、JPA 或 Spring 等 Java 框架时,我通常可以通过 xml 文件进行配置或将注释直接放在我的类中.
When I look at Java frameworks like Hibernate, JPA, or Spring, I usually have the possibility to make my configuration via an xml-file or put annotations directly in my classes.
我一直在问自己应该走哪条路.
I am cosistently asking myself what way should I go.
当我使用注释时,我将类及其配置放在一起,但使用 xml 我可以更全面地了解我的系统,因为我可以看到所有类配置.
When I use annotations, I have the class and its configuration together but with an xml I get a bigger picture of my system because I can see all class-configurations.
注释也会以某种方式编译,我猜它应该比解析 xml 更快,但另一方面,如果我想更改我的配置,我必须重新编译它而不是仅仅更改 xml 文件(这可能对于客户端的生产环境变得更加方便).
Annotations also get compiled in some way I guess and it should be quicker than parsing the xml, but on the other hand, if I want to change my configuration I have to recompile it instead of just changing the xml file (which might become even more handy for production environments on customer side).
因此,在查看我的观点时,我倾向于采用 xml 方式.但是在查看论坛或教程时,通常会使用注释.
So, when looking at my points, I tend to go the xml-way. But when looking at forums or tutorials usually annotations are used.
你的优点和缺点是什么?
What are your pros and cons?
一个好的经验法则:任何你可以看到自己想要更改而不需要完全重新部署(例如调整性能参数)的东西都应该真正进入软配置"" 例如 XML 文件.任何实际上永远不会改变的东西——或者只有在你不得不改变代码的那种情况下——都可以合理地放在注释中.
A good rule of thumb: anything you can see yourself wanting to change without a full redeploy (e.g. tweaking performance parameters) should really go in something "soft-configurable" such as an XML file. Anything which is never realistically going to change - or only in the sort of situation where you'll be having to change the code anyway - can reasonably be in an annotation.
忽略注释和 XML 之间性能差异的任何想法 - 除非您的配置绝对大量差异将是微不足道的.
Ignore any ideas of performance differences between annotations and XML - unless your configuration is absolutely massive the difference will be insignificant.
专注于灵活性和可读性.
Concentrate on flexibility and readability.
这篇关于与 xml 配置文件相比,注释(非编译器)的优点/缺点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!