我们可以使用注释的主要领域是什么?该特性是基于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).

希望我已经让您了解了在不同框架中如何使用注释。

其他回答

它是基于XML的替代品吗 配置吗?

不完全如此,但是与代码结构紧密对应的配置(例如JPA映射或Spring中的依赖注入)通常可以用注释代替,这样通常就不那么冗长、烦人和痛苦了。几乎所有著名的框架都进行了这种转换,尽管旧的XML配置通常仍然是一种选项。

注释有两个视图

在用户视图中,大多数时候,注释就像一个快捷键,为你节省一些按键,或使你的程序更具可读性 从供应商的角度来看,处理器对注释的看法更像是一个轻量级的“接口”,你的程序确实要面对一些东西,但没有显式地“实现”特定的接口(这里也就是注释)。

例如,在jpa中,您可以定义如下内容

@Entity class Foo {...}

而不是

class Foo implements Entity {...}

两者都说了同样的话"Foo是一个实体类"

它通过(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/

注释是添加到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).

希望我已经让您了解了在不同框架中如何使用注释。

像Hibernate这样的框架需要大量的配置/映射,大量使用annotation。

看一看Hibernate注解