我们可以使用注释的主要领域是什么?该特性是基于XML的配置的替代品吗?
当前回答
注释是添加到Java源文件中的元数据(关于数据的数据)的一种形式。框架主要使用它们来简化客户端代码的集成。下面是我脑海中浮现的几个真实的例子:
JUnit 4 - you add the @Test annotation to each test method you want the JUnit runner to run. There are also additional annotations to do with setting up testing (like @Before and @BeforeClass). All these are processed by the JUnit runner, which runs the tests accordingly. You could say it's an replacement for XML configuration, but annotations are sometimes more powerful (they can use reflection, for example) and also they are closer to the code they are referencing to (the @Test annotation is right before the test method, so the purpose of that method is clear - serves as documentation as well). XML configuration on the other hand can be more complex and can include much more data than annotations can. Terracotta - uses both annotations and XML configuration files. For example, the @Root annotation tells the Terracotta runtime that the annotated field is a root and its memory should be shared between VM instances. The XML configuration file is used to configure the server and tell it which classes to instrument. Google Guice - an example would be the @Inject annotation, which when applied to a constructor makes the Guice runtime look for values for each parameter, based on the defined injectors. The @Inject annotation would be quite hard to replicate using XML configuration files, and its proximity to the constructor it references to is quite useful (imagine having to search to a huge XML file to find all the dependency injections you have set up).
希望我已经让您了解了在不同框架中如何使用注释。
其他回答
它通过(a)编译器检查或(b)代码分析附加有关代码的其他信息
**
以下是内置注释::2类型
**
类型1)应用于java代码的注释:
@Override // gives error if signature is wrong while overriding.
Public boolean equals (Object Obj)
@Deprecated // indicates the deprecated method
Public doSomething()....
@SuppressWarnings() // stops the warnings from printing while compiling.
SuppressWarnings({"unchecked","fallthrough"})
类型2)应用于其他注释的注释:
@Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at run-time through reflection.
@Documented - Marks another annotation for inclusion in the documentation.
@Target - Marks another annotation to restrict what kind of java elements the annotation may be applied to
@Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).
**
自定义注解::
** http://en.wikipedia.org/wiki/Java_annotation#Custom_annotations
为了更好地理解,请尝试下面的链接:用例子详细说明
http://www.javabeat.net/2007/08/annotations-in-java-5-0/
它对于注释类非常有用,无论是在方法、类或字段级别,都是关于类的一些与类不太相关的东西。
您可以有自己的注释,用于将某些类标记为仅测试使用。它可能只是为了文档的目的,或者您可以在编译产品候选发行版时通过过滤它来强制执行它。
你可以使用注释来存储一些元数据,就像在插件框架中一样,例如插件的名称。
它只是另一种工具,它有很多用途。
JPA(来自Java EE 5)是(过度)使用注释的一个很好的例子。Java EE 6还将在很多新领域引入注释,比如RESTful webservices和每个老Servlet API下的新注释。
这里有一些资源:
Sun - Java持久性API Java EE 5教程- JPA 介绍Java EE 6平台(查看所有三个页面)。
注释不仅可以接管配置细节,而且还可以用来控制行为。您可以在Java EE 6的JAX-RS示例中看到这一点。
像Hibernate这样的框架需要大量的配置/映射,大量使用annotation。
看一看Hibernate注解
注释可以用作外部配置文件的替代品,但不能被视为完全替代。您可以找到许多使用annotationi替换配置文件的示例,如Hibernate、JPA、EJB 3以及Java EE中包含的几乎所有技术。
Anyway this is not always good choice. The purpose of using configuration files is usually to separate the code from the details of the environment where the application is running. In such situations, and mostly when the configuration is used to map the application to the structure of an external system, annotation are not a good replacement for configuration file, as they bring you to include the details of the external system inside the source code of your application. Here external files are to be considered the best choice, otherwise you'll need to modify the source code and to recompile every time you change a relevant detail in the execution environment.
注释更适合用额外的信息装饰源代码,这些信息在编译时和运行时指示处理工具以特殊的方式处理类和类结构。@Override和JUnit的@Test是这种用法的好例子,已经在其他回答中详细解释过了。
最后,规则总是一样的:把随源代码一起变化的东西放在源代码内部,把独立于源代码变化的东西放在源代码外部。
推荐文章
- List、List<?>, List<T>, List<E>, List<Object>
- 在Java中转换字符串到日历对象
- 在java中使用wait()和notify()的简单场景
- 增强的for循环中的Null检查
- c# vs Java Enum(适合c#新手)
- 如何在不强制转换的情况下将double转换为long ?
- for循环和for-each循环在性能上有区别吗?
- 你如何比较两个版本的字符串在Java?
- 为什么在Java和。net中不能修改字符串?
- java.util.Random真的那么随机吗?我怎么能生成52!(阶乘)可能的序列?
- ZoneOffset之间的区别是什么。UTC和ZoneId.of(“UTC”)?
- 类未找到:IntelliJ中的空测试套件
- 将JAR文件添加到Spark作业- Spark -submit
- REST API - dto还是不是?
- JavaFX应用程序图标