我们可以使用注释的主要领域是什么?该特性是基于XML的配置的替代品吗?
当前回答
注释可以用作外部配置文件的替代品,但不能被视为完全替代。您可以找到许多使用annotationi替换配置文件的示例,如Hibernate、JPA、EJB 3以及Java EE中包含的几乎所有技术。
Anyway this is not always good choice. The purpose of using configuration files is usually to separate the code from the details of the environment where the application is running. In such situations, and mostly when the configuration is used to map the application to the structure of an external system, annotation are not a good replacement for configuration file, as they bring you to include the details of the external system inside the source code of your application. Here external files are to be considered the best choice, otherwise you'll need to modify the source code and to recompile every time you change a relevant detail in the execution environment.
注释更适合用额外的信息装饰源代码,这些信息在编译时和运行时指示处理工具以特殊的方式处理类和类结构。@Override和JUnit的@Test是这种用法的好例子,已经在其他回答中详细解释过了。
最后,规则总是一样的:把随源代码一起变化的东西放在源代码内部,把独立于源代码变化的东西放在源代码外部。
其他回答
注释可以用作外部配置文件的替代品,但不能被视为完全替代。您可以找到许多使用annotationi替换配置文件的示例,如Hibernate、JPA、EJB 3以及Java EE中包含的几乎所有技术。
Anyway this is not always good choice. The purpose of using configuration files is usually to separate the code from the details of the environment where the application is running. In such situations, and mostly when the configuration is used to map the application to the structure of an external system, annotation are not a good replacement for configuration file, as they bring you to include the details of the external system inside the source code of your application. Here external files are to be considered the best choice, otherwise you'll need to modify the source code and to recompile every time you change a relevant detail in the execution environment.
注释更适合用额外的信息装饰源代码,这些信息在编译时和运行时指示处理工具以特殊的方式处理类和类结构。@Override和JUnit的@Test是这种用法的好例子,已经在其他回答中详细解释过了。
最后,规则总是一样的:把随源代码一起变化的东西放在源代码内部,把独立于源代码变化的东西放在源代码外部。
它是基于XML的替代品吗 配置吗?
不完全如此,但是与代码结构紧密对应的配置(例如JPA映射或Spring中的依赖注入)通常可以用注释代替,这样通常就不那么冗长、烦人和痛苦了。几乎所有著名的框架都进行了这种转换,尽管旧的XML配置通常仍然是一种选项。
它通过(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 EE 5倾向于使用注释而不是XML配置。例如,在EJB3中,EJB方法上的事务属性是使用注释指定的。他们甚至使用注释将pojo标记为ejb,并将特定的方法指定为生命周期方法,而不需要接口的实现。
推荐文章
- Java Regex捕获组
- Openssl不被视为内部或外部命令
- 如何添加自定义方法到Spring Data JPA
- 如何在Ubuntu中设置Java环境路径
- 无法执行dex:在Eclipse中超过GC开销限制
- 有人能解释一下JPA和Hibernate中的mappedBy吗?
- 是什么导致JNI调用变慢?
- Java中的&和&&有什么区别?
- 使用Java的Collections.singletonList()?
- Maven使用多个src目录编译
- 导入时无法解析符号
- 用谓词限制流
- 为特定的代码行禁用特定的Checkstyle规则?
- Java中生成UUID字符串的有效方法(UUID. randomuuid ().toString()不带破折号)
- malformedurlexception:没有协议