Java Reference
In-Depth Information
public void save(){}
}
The annotations have been integrated into the Java language since JDK 5 and they
are now widely used in many APIs. To avoid redefining some annotations in several
APIs, theJCPdevelopedthecommonannotations fortheJavaplatform specification
with the goal of regrouping annotations that are common to different Java EE APIs,
which avoids redundancy and facilitates the maintenance of regrouped annotations.
In the following code, we have the example of the
@Resource
annotation from the
common annotations for the Java platform API , which permits us to access an ob-
ject of type
SessionContext
in a web container and in an EJB container.
@Stateless
public class MySessionBean {
@javax.annotation.Resource
private SessionContext sctx;
//...
}
@ManagedBean
public class MyJsfManagedBean {
@javax.annotation.Resource
private SessionContext sctx;
//...
}
Building your own annotation
Although there are already several annotations, Java offers the opportunity to create
new custom annotations if you need. To do this, you should know that an annotation
is declared as a Java interface. The only difference is that, in the case of the annota-
tion the keyword
interface
must be preceded by the character
@
. The following
code shows the declaration of the custom annotation
Unfinished
. This annotation
contains a parameter named
message
whose default value is
Nothing has been
done
.