我们可以使用注释的主要领域是什么?该特性是基于XML的配置的替代品吗?
当前回答
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)编译器检查或(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/
像Hibernate这样的框架需要大量的配置/映射,大量使用annotation。
看一看Hibernate注解
下面是一些可以使用注释的地方。
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
有关注释的更多详细信息,请参考此链接。
您可以参考此链接,了解如何使用注释构建简单的测试套件。
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标准(持久性、依赖项注入等等)都使用这种注释处理,以便提供必要的元数据和配置信息。
它对于注释类非常有用,无论是在方法、类或字段级别,都是关于类的一些与类不太相关的东西。
您可以有自己的注释,用于将某些类标记为仅测试使用。它可能只是为了文档的目的,或者您可以在编译产品候选发行版时通过过滤它来强制执行它。
你可以使用注释来存储一些元数据,就像在插件框架中一样,例如插件的名称。
它只是另一种工具,它有很多用途。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap