what's annotations is in java? what's the relationship between annotation and reflection?
2015-04-09 19:00
316 查看
Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are classes, fields and methods. Asking an object
for its meta-object (e.g.
is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g.
Introspection and annotations belong to what is called reflection and meta-programming.
An annotation needs to be interpreted in one way or another to be useful. Annotations can be interpreted at development-time by the IDE or the compiler, or at run-time by
a framework.
Annotation processing is a very powerful mechanism and can be used in a lot of different ways:
to describe constraints or usage of an element: e.g.
to describe the "nature" of an element, e.g.
to describe the behavior of an element:
to describe how to process the element:
In all cases, an annotation is used to describe the element and clarify its meaning.
Prior to JDK5, information that is now expressed with annotations needed to be stored somewhere else, and XML files were frequently used. But it is more convenient to use annotations because they will belong to the Java code itself, and are hence much easier
to manipulate than XML.
Usage of annotations:
Documentation, e.g. XDoclet
Compilation
IDE
Testing framework, e.g. JUnit
IoC container e.g. as Spring
Serialization, e.g. XML
Aspect-oriented programming (AOP), e.g. Spring AOP
Application servers, e.g. EJB container, Web Service
Object-relational mapping (ORM), e.g. Hibernate, JPA
and many more...
...have a look for instance at the project Lombok, which uses annotations to
define how to generate
There are mutiple applications for Java's annotations. First of all, they may used by the compiler (or compiler extensions). Consider for example the Override annotation:
This one is actually built into the Java JDK. The compiler will signal an error, if some method is tagged with it, which does not override a method inherited from a base class. This annotation may be
helpful in order to avoid the common mistake, where you actually intend to override a method, but fail to do so, because the signature given in your method does not match the signature of the method being overridden:
The forthcoming JDK7 will allow annotations on any type. There are already proposals to use this feature for compiler annotations such as NotNull, like in:
which might allow the compiler to warn you about improper/unchecked uses of variables and nullvalues.
Another, more advanced application for annotations involves reflection and annotation processing at run-time. This is (I think) what you had in mind when you speak of annotations as "replacement for XML based configuration". This is the kind of annotation processing
used, for example, by various frameworks and JCP standards (persistence, dependency injection, you name it) in order to provide the necessary meta-data and configuration information.
Annotations are a form of metadata (data about data) added to a Java source file. They are largely used by frameworks to simplify the integration of client code. A couple of real world examples of the top of my head:
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 `@Text` 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).
Hopefully I've given you a flavour of how annotations are used in different frameworks.
for its meta-object (e.g.
anObj.getClass())
is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g.
aClass.getAnnotations).
Introspection and annotations belong to what is called reflection and meta-programming.
An annotation needs to be interpreted in one way or another to be useful. Annotations can be interpreted at development-time by the IDE or the compiler, or at run-time by
a framework.
Annotation processing is a very powerful mechanism and can be used in a lot of different ways:
to describe constraints or usage of an element: e.g.
@Deprecated, @Override, or
@NotNull
to describe the "nature" of an element, e.g.
@Entity, @TestCase, @WebService
to describe the behavior of an element:
@Statefull, @Transaction
to describe how to process the element:
@Column, @XmlElement
In all cases, an annotation is used to describe the element and clarify its meaning.
Prior to JDK5, information that is now expressed with annotations needed to be stored somewhere else, and XML files were frequently used. But it is more convenient to use annotations because they will belong to the Java code itself, and are hence much easier
to manipulate than XML.
Usage of annotations:
Documentation, e.g. XDoclet
Compilation
IDE
Testing framework, e.g. JUnit
IoC container e.g. as Spring
Serialization, e.g. XML
Aspect-oriented programming (AOP), e.g. Spring AOP
Application servers, e.g. EJB container, Web Service
Object-relational mapping (ORM), e.g. Hibernate, JPA
and many more...
...have a look for instance at the project Lombok, which uses annotations to
define how to generate
equalsor
hashCodemethods.
There are mutiple applications for Java's annotations. First of all, they may used by the compiler (or compiler extensions). Consider for example the Override annotation:
class Foo { @Override public boolean equals(Object other) { return ...; } }
This one is actually built into the Java JDK. The compiler will signal an error, if some method is tagged with it, which does not override a method inherited from a base class. This annotation may be
helpful in order to avoid the common mistake, where you actually intend to override a method, but fail to do so, because the signature given in your method does not match the signature of the method being overridden:
class Foo { @Override public boolean equals(Foo other) { // Compiler signals an error for this one return ...; } }
The forthcoming JDK7 will allow annotations on any type. There are already proposals to use this feature for compiler annotations such as NotNull, like in:
public void processSomething(@NotNull String text) { ... }
which might allow the compiler to warn you about improper/unchecked uses of variables and nullvalues.
Another, more advanced application for annotations involves reflection and annotation processing at run-time. This is (I think) what you had in mind when you speak of annotations as "replacement for XML based configuration". This is the kind of annotation processing
used, for example, by various frameworks and JCP standards (persistence, dependency injection, you name it) in order to provide the necessary meta-data and configuration information.
Annotations are a form of metadata (data about data) added to a Java source file. They are largely used by frameworks to simplify the integration of client code. A couple of real world examples of the top of my head:
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 `@Text` 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).
Hopefully I've given you a flavour of how annotations are used in different frameworks.
相关文章推荐
- Java WEB: what's the difference between redirect and dispatch?
- What is the ARGB int encoding of pixels in Java's AWT?
- what's up in java 7
- J2ME技術使"Java無所不在" (一)
- 关于C++&Java中命令行编译的问题
- JAVA多态性<==>我和网友交流实录
- Java Q&A: 使用Observer模式
- PHP & Java(2)
- What doesn't kill me makes me stronger!!
- (Page 3 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- PHP & Java(1)
- JAVA&JSP_friend
- JVM - Concept and Lifetime (From book "Inside the Java Virtual Machine")
- What's New in the .NET Framework 2.0 Beta
- [Java细节]"hi there".equals("cheers !") == true
- 有感"Java is not platform-independent, it is the platform"
- Seeing What You've Drawn
- Java Q&A: 使用Factory Method模式
- Java Q&A: 抽象类和接口 - 实战练习
- What's New in System.Xml for Visual Studio 2005 and the .NET Framework 2.0 Release