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

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

其他回答

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示例中看到这一点。

Java的注释有多种应用程序。首先,它们可能被编译器(或编译器扩展)使用。以Override注释为例:

class Foo {

    @Override public boolean equals(Object other) {
        return ...;
    }
}

它实际上内置于Java JDK中。如果某个方法被标记为错误,编译器将发出错误信号,该方法不会覆盖从基类继承的方法。这个注释可能有助于避免常见的错误,即你实际上打算覆盖一个方法,但由于方法中给出的签名与被覆盖方法的签名不匹配而失败:

class Foo {

    @Override public boolean equals(Foo other) {  // Compiler signals an error for this one
        return ...;
    }
}

从JDK7开始,任何类型都允许标注。这个特性现在可以用于编译器注释,比如NotNull,比如:

public void processSomething(@NotNull String text) {
    ...
}

这允许编译器警告你不适当/未检查的变量和空值的使用。

另一个更高级的注释应用程序涉及运行时的反射和注释处理。当您说注释是“基于XML的配置的替代品”时,这就是您所想到的(我认为)。例如,各种框架和JCP标准(持久性、依赖项注入等等)都使用这种注释处理,以便提供必要的元数据和配置信息。

下面是一些可以使用注释的地方。

a. Annotations can be used by compiler to detect errors and suppress warnings
b. Software tools can use annotations to generate code, xml files, documentation etc., For example, Javadoc use annotations while generating java documentation for your class.
c. Runtime processing of the application can be possible via annotations.
d. You can use annotations to describe the constraints (Ex: @Null, @NotNull, @Max, @Min, @Email).
e. Annotations can be used to describe type of an element. Ex: @Entity, @Repository, @Service, @Controller, @RestController, @Resource etc.,
f. Annotation can be used to specify the behaviour. Ex: @Transactional, @Stateful
g. Annotation are used to specify how to process an element. Ex: @Column, @Embeddable, @EmbeddedId
h. Test frameworks like junit and testing use annotations to define test cases (@Test), define test suites (@Suite) etc.,
i. AOP (Aspect Oriented programming) use annotations (@Before, @After, @Around etc.,)
j. ORM tools like Hibernate, Eclipselink use annotations

有关注释的更多详细信息,请参考此链接。

您可以参考此链接,了解如何使用注释构建简单的测试套件。

注释是元对象,可以用来描述其他元对象。元对象包括类、字段和方法。向一个对象请求它的元对象(例如anObj.getClass())被称为内省。内省可以更进一步,我们可以问元对象它的注释是什么(例如aClass.getAnnotations)。内省和注释属于所谓的反射和元编程。

注释需要以一种或另一种方式进行解释才能有用。注释可以在开发时由IDE或编译器解释,也可以在运行时由框架解释。

注释处理是一种非常强大的机制,可以以许多不同的方式使用:

描述一个元素的约束或用法:例如@Deprecated, @Override,或@NotNull 来描述一个元素的“性质”,例如@Entity, @TestCase, @WebService 来描述元素的行为:@Statefull, @Transaction 来描述如何处理元素:@Column, @XmlElement

在所有情况下,注释都用于描述元素并阐明其含义。

在JDK5之前,现在用注释表示的信息需要存储在其他地方,并且经常使用XML文件。但是使用注释更方便,因为它们属于Java代码本身,因此比XML更容易操作。

注释的使用:

文档,例如XDoclet 编译 IDE 测试框架,例如JUnit IoC容器,例如Spring 序列化,例如XML 面向方面编程(AOP),例如Spring AOP 应用服务器,例如EJB容器、Web服务 对象-关系映射(ORM),例如Hibernate、JPA 还有更多……

...比如Lombok项目,它使用注释来定义如何生成equals或hashCode方法。

它对于注释类非常有用,无论是在方法、类或字段级别,都是关于类的一些与类不太相关的东西。

您可以有自己的注释,用于将某些类标记为仅测试使用。它可能只是为了文档的目的,或者您可以在编译产品候选发行版时通过过滤它来强制执行它。

你可以使用注释来存储一些元数据,就像在插件框架中一样,例如插件的名称。

它只是另一种工具,它有很多用途。