That is a very interesting question.
Annotations are meta-meta-object which can be used to describe other meta-object. Meta-object are class, field and method. Asking an object for its meta-object (e.g.
An annotation needs to be interpreted in one way or another to to be useful. Annotations can be interpreted at development-time by the IDE or the compiler or at run-time by a framework.
Annotation is a very powerful mechanism and can be used in a lot of different ways:
Prior to JDK5, the information that is now contained in the 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:
http://stackoverflow.com/questions/1372876/how-and-where-are-annotations-used-in-java
Annotations are meta-meta-object which can be used to describe other meta-object. Meta-object are class, field and method. Asking an object 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 reflexion and meta-programming. An annotation needs to be interpreted in one way or another to to be useful. Annotations can be interpreted at development-time by the IDE or the compiler or at run-time by a framework.
Annotation 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
Prior to JDK5, the information that is now contained in the 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...
equals
or hashCode
methods.http://stackoverflow.com/questions/1372876/how-and-where-are-annotations-used-in-java
No comments:
Post a Comment