我们可以使用注释的主要领域是什么?该特性是基于XML的配置的替代品吗?
当前回答
下面是一些可以使用注释的地方。
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
有关注释的更多详细信息,请参考此链接。
您可以参考此链接,了解如何使用注释构建简单的测试套件。
其他回答
它对于注释类非常有用,无论是在方法、类或字段级别,都是关于类的一些与类不太相关的东西。
您可以有自己的注释,用于将某些类标记为仅测试使用。它可能只是为了文档的目的,或者您可以在编译产品候选发行版时通过过滤它来强制执行它。
你可以使用注释来存储一些元数据,就像在插件框架中一样,例如插件的名称。
它只是另一种工具,它有很多用途。
下面是一些可以使用注释的地方。
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
有关注释的更多详细信息,请参考此链接。
您可以参考此链接,了解如何使用注释构建简单的测试套件。
它是基于XML的替代品吗 配置吗?
不完全如此,但是与代码结构紧密对应的配置(例如JPA映射或Spring中的依赖注入)通常可以用注释代替,这样通常就不那么冗长、烦人和痛苦了。几乎所有著名的框架都进行了这种转换,尽管旧的XML配置通常仍然是一种选项。
注释是元对象,可以用来描述其他元对象。元对象包括类、字段和方法。向一个对象请求它的元对象(例如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方法。
注释有两个视图
在用户视图中,大多数时候,注释就像一个快捷键,为你节省一些按键,或使你的程序更具可读性 从供应商的角度来看,处理器对注释的看法更像是一个轻量级的“接口”,你的程序确实要面对一些东西,但没有显式地“实现”特定的接口(这里也就是注释)。
例如,在jpa中,您可以定义如下内容
@Entity class Foo {...}
而不是
class Foo implements Entity {...}
两者都说了同样的话"Foo是一个实体类"
推荐文章
- 在流中使用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