我们可以使用注释的主要领域是什么?该特性是基于XML的配置的替代品吗?
当前回答
像Hibernate这样的框架需要大量的配置/映射,大量使用annotation。
看一看Hibernate注解
其他回答
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标准(持久性、依赖项注入等等)都使用这种注释处理,以便提供必要的元数据和配置信息。
它是基于XML的替代品吗 配置吗?
不完全如此,但是与代码结构紧密对应的配置(例如JPA映射或Spring中的依赖注入)通常可以用注释代替,这样通常就不那么冗长、烦人和痛苦了。几乎所有著名的框架都进行了这种转换,尽管旧的XML配置通常仍然是一种选项。
Java中的注释提供了一种描述类、字段和方法的方法。本质上,它们是添加到Java源文件中的一种元数据形式,它们不能直接影响程序的语义。但是,注释可以在运行时使用反射读取&这个过程被称为内省。然后,它可以用来修改类、字段或方法。
这个特性,经常被库和sdk (hibernate, JUnit, Spring Framework)利用来简化或减少程序员在使用这些库或sdk时需要做的代码量。因此,可以公平地说,注释和反射在Java中是齐头并进的。
我们还将注释的可用性限制在编译时或运行时。下面是一个创建自定义注释的简单示例
Driver.java
package io.hamzeen;
import java.lang.annotation.Annotation;
public class Driver {
public static void main(String[] args) {
Class<TestAlpha> obj = TestAlpha.class;
if (obj.isAnnotationPresent(IssueInfo.class)) {
Annotation annotation = obj.getAnnotation(IssueInfo.class);
IssueInfo testerInfo = (IssueInfo) annotation;
System.out.printf("%nType: %s", testerInfo.type());
System.out.printf("%nReporter: %s", testerInfo.reporter());
System.out.printf("%nCreated On: %s%n%n",
testerInfo.created());
}
}
}
TestAlpha.java
package io.hamzeen;
import io.hamzeen.IssueInfo;
import io.hamzeen.IssueInfo.Type;
@IssueInfo(type = Type.IMPROVEMENT, reporter = "Hamzeen. H.")
public class TestAlpha {
}
IssueInfo.java
package io.hamzeen;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Hamzeen. H.
* @created 10/01/2015
*
* IssueInfo annotation definition
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface IssueInfo {
public enum Type {
BUG, IMPROVEMENT, FEATURE
}
Type type() default Type.BUG;
String reporter() default "Vimesh";
String created() default "10/01/2015";
}
像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
有关注释的更多详细信息,请参考此链接。
您可以参考此链接,了解如何使用注释构建简单的测试套件。
推荐文章
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 如何在POM.xml中引用环境变量?
- 如何在android中复制一个文件?
- 将整数转换为字符串,以逗号表示千
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?
- 什么是NullPointerException,我如何修复它?
- 在Java中使用“final”修饰符
- 无法在Flutter上找到捆绑的Java版本
- 如何在Kotlin解析JSON ?
- 如何在新的材质主题中改变背面箭头的颜色?