I having hard time understanding importance and benefits of Annotations and so have two questions regarding them:
我很难理解注释的重要性和好处,因此有两个问题:
- What are the benefits of Annotations as compared to XML Configuration?
- 与XML配置相比,注解有什么好处?
- How do Annotations work internally?
- 注解在内部是如何工作的?
- Is it fair enough to say that annotation binds application tightly whereas with XML Configuration Application is loosely coupled?
- 是否公平地说注释紧密地绑定应用程序,而与XML配置应用程序松散耦合?
Would appreciate pros and cons comparison with XML Configuration with example so that it would be much more helpful for me to understand.
非常感谢您与XML配置的优缺点对比,以便我能更好的理解。
Regards.
的问候。
3 个解决方案
#1
0
Both annotations and XML descriptors are used to describe some metadata on top of regular code. The primary difference is that in case of annotations you only have to deal with one file which includes code and metadata. It is also a big advantage of annotations as it reduces number of moving parts and increases productivity.
注释和XML描述符都用于描述常规代码之上的一些元数据。主要的区别在于,对于注释,您只需处理一个包含代码和元数据的文件。它也是注解的一大优势,因为它减少了移动部件的数量并提高了生产率。
On the other hand, the drawback of annotations is that they bind together the code and the system or framework that operates using those annotations. That makes it harder to separate those in future.
另一方面,注释的缺点是它们将代码和使用这些注释操作的系统或框架绑定在一起。这使得在未来很难将它们分开。
For example, if you use Hibernate Annotations, you bind your model objects with Hibernate. If you choose to switch to different framework, you will have to rip out Hibernate annotations from the code.
例如,如果您使用Hibernate注解,您可以用Hibernate绑定模型对象。如果您选择切换到不同的框架,您将不得不从代码中删除Hibernate注释。
But practically, it's not that likely that you will be changing frameworks that often. There are usually many other reasons why changing framework on existing code base may be hard. So often annotations is a good choice.
但实际上,你不太可能经常改变框架。在现有代码基础上更改框架通常还有很多其他原因。注释通常是一个不错的选择。
As to how they work, annotations are a part of the language and are processed by compiler and other tools and, depending on retention, can be included in produced bytecode for use at runtime. Ultimately, it's up to consumer to decide on how to use annotations.
至于它们是如何工作的,注释是语言的一部分,由编译器和其他工具处理,根据保留程度,可以包含在生成的字节码中,以便在运行时使用。最终,由使用者决定如何使用注解。
#2
4
For your 1st question,
为你的第一个问题,
- Xml configuration versus Annotation based configuration
- Xml配置与基于注释的配置
Personally, I feel, there are two criteria's
我个人认为,有两个标准
- Can annotations simplify the metadata ?
If annotations do not reduce the amount of metadata that you have to provide (in most cases they do), then you shouldn’t use annotation.
如果注释没有减少您必须提供的元数据的数量(在大多数情况下是这样),那么您不应该使用注释。
-
Can changes to the metadata break behavior in your application?
是否可以在应用程序中更改元数据中断行为?
If not, then you can feel comfortable applying the change while the system is running in production. External config files are the best place for the metadata in this case because you don’t want to have to recompile your code to make the change.
如果不是,那么当系统在生产环境中运行时,您可以放心地应用更改。在这种情况下,外部配置文件是元数据的最佳位置,因为您不需要重新编译代码来进行更改。
For your 2nd question,
对于你的第二个问题,
- How Do Annotations Work?
- 注释如何工作?
Important Links :
重要的链接:
- What are annotations and how do they actually work for frameworks like Spring?
- 什么是注释,它们是如何在Spring这样的框架中工作的?
#3
0
To answer the first question, IMO the greatest benefit is the potential for compiler integration. I can write an annotation processor that can validate some semantics related to the application of the annotation. That kind of compile-time checking is not possible (or would at least be way more difficult) if the same information was instead part of an XML document.
要回答第一个问题,我认为最大的好处是编译器集成的潜力。我可以编写一个注释处理器,它可以验证与注释应用程序相关的一些语义。如果相同的信息是XML文档的一部分,那么这种编译时检查是不可能的(或者至少要困难得多)。
To answer the second question, annotations don't really "work" internally, per se, in the sense that they don't have any inherent execution semantics. They are source level entities that may or may not be retained in the classfile. The can be processed during compilation of the source, and if they were retained in the classfile, can be accessed via reflection.
要回答第二个问题,注释本身并不真正在内部“工作”,因为它们没有任何固有的执行语义。它们是源级实体,可以保留在类文件中,也可以不保留。可以在编译源代码时处理它们,如果它们保留在类文件中,则可以通过反射访问它们。
#1
0
Both annotations and XML descriptors are used to describe some metadata on top of regular code. The primary difference is that in case of annotations you only have to deal with one file which includes code and metadata. It is also a big advantage of annotations as it reduces number of moving parts and increases productivity.
注释和XML描述符都用于描述常规代码之上的一些元数据。主要的区别在于,对于注释,您只需处理一个包含代码和元数据的文件。它也是注解的一大优势,因为它减少了移动部件的数量并提高了生产率。
On the other hand, the drawback of annotations is that they bind together the code and the system or framework that operates using those annotations. That makes it harder to separate those in future.
另一方面,注释的缺点是它们将代码和使用这些注释操作的系统或框架绑定在一起。这使得在未来很难将它们分开。
For example, if you use Hibernate Annotations, you bind your model objects with Hibernate. If you choose to switch to different framework, you will have to rip out Hibernate annotations from the code.
例如,如果您使用Hibernate注解,您可以用Hibernate绑定模型对象。如果您选择切换到不同的框架,您将不得不从代码中删除Hibernate注释。
But practically, it's not that likely that you will be changing frameworks that often. There are usually many other reasons why changing framework on existing code base may be hard. So often annotations is a good choice.
但实际上,你不太可能经常改变框架。在现有代码基础上更改框架通常还有很多其他原因。注释通常是一个不错的选择。
As to how they work, annotations are a part of the language and are processed by compiler and other tools and, depending on retention, can be included in produced bytecode for use at runtime. Ultimately, it's up to consumer to decide on how to use annotations.
至于它们是如何工作的,注释是语言的一部分,由编译器和其他工具处理,根据保留程度,可以包含在生成的字节码中,以便在运行时使用。最终,由使用者决定如何使用注解。
#2
4
For your 1st question,
为你的第一个问题,
- Xml configuration versus Annotation based configuration
- Xml配置与基于注释的配置
Personally, I feel, there are two criteria's
我个人认为,有两个标准
- Can annotations simplify the metadata ?
If annotations do not reduce the amount of metadata that you have to provide (in most cases they do), then you shouldn’t use annotation.
如果注释没有减少您必须提供的元数据的数量(在大多数情况下是这样),那么您不应该使用注释。
-
Can changes to the metadata break behavior in your application?
是否可以在应用程序中更改元数据中断行为?
If not, then you can feel comfortable applying the change while the system is running in production. External config files are the best place for the metadata in this case because you don’t want to have to recompile your code to make the change.
如果不是,那么当系统在生产环境中运行时,您可以放心地应用更改。在这种情况下,外部配置文件是元数据的最佳位置,因为您不需要重新编译代码来进行更改。
For your 2nd question,
对于你的第二个问题,
- How Do Annotations Work?
- 注释如何工作?
Important Links :
重要的链接:
- What are annotations and how do they actually work for frameworks like Spring?
- 什么是注释,它们是如何在Spring这样的框架中工作的?
#3
0
To answer the first question, IMO the greatest benefit is the potential for compiler integration. I can write an annotation processor that can validate some semantics related to the application of the annotation. That kind of compile-time checking is not possible (or would at least be way more difficult) if the same information was instead part of an XML document.
要回答第一个问题,我认为最大的好处是编译器集成的潜力。我可以编写一个注释处理器,它可以验证与注释应用程序相关的一些语义。如果相同的信息是XML文档的一部分,那么这种编译时检查是不可能的(或者至少要困难得多)。
To answer the second question, annotations don't really "work" internally, per se, in the sense that they don't have any inherent execution semantics. They are source level entities that may or may not be retained in the classfile. The can be processed during compilation of the source, and if they were retained in the classfile, can be accessed via reflection.
要回答第二个问题,注释本身并不真正在内部“工作”,因为它们没有任何固有的执行语义。它们是源级实体,可以保留在类文件中,也可以不保留。可以在编译源代码时处理它们,如果它们保留在类文件中,则可以通过反射访问它们。