File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Annotation guide Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java Annotation guide" Watch "Java Annotation guide" New topic
Author

Java Annotation guide

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Hi Everyone,

I need to know what are the annotation. Is there any good resource for this.

Thanks
Pawan
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Pawan,

annotations were introduced to the Java language with version 1.5. In short they are kind of comments or hints you can insert into Java code. The big advantage is that they can easily be processed and are handled like Java classes which wouldn't be as easy or even possible with ordinary code comments. Oh, and you can of course define your own annotations.

So you can insert any kind of meta data directly into the source code and it's easy for any tools to recognize and handle the relevant meta data. Take a look at Sun's tutorial for more information on annotations

Marco
[ May 28, 2008: Message edited by: Marco Ehrentreich ]
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Thanks a lot Marco.
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Marco,
http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

link has lines

we need not to create DD for EJBs, we can do deployment settings using annotations, but my question is that for change in setting, we have to change java file and later we have to compile it again. Is it not a tough job.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
I'm not familiar with the annotations defined for EJBs, but there are several ways to use annotations.

One way would be like you described - using the annotations in the class files directly for the deployment. You're right that that would indeed require recompilation for changes, and thus would be rather inconvenient.

Another way would be to create a DD from the annotations at build time. The DD file would reflect what's in the source -which presumably are reasonable defaults-, and could then be used as the basis for further customization, if necessary.

One could also envision a scheme where what's in the class files is used for deployment, but could be overridden by a DD file if necessary. This is actually the approach taken by the upcoming servlets/JSP specification - it defines a number of annotations, but allows those to be overridden by the web.xml file.

Either of these last two options would be much better than the first one.


Ping & DNS - my free Android networking tools app
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

With EJB 3.0 (and I think you'll be using version 3.0 if you're using annotations) you have to possibilities to give deployment and container instructions. You can use the mentioned annotations and you can still use an XML descriptor file as used in older versions of EJB, i.e version 2.x.

If you need or want to specify other parameters after compilation you can use the XML descriptor for these tasks. The options specified in the descriptor file take precedence over the same options already specified with annotations directly in the source code files.

So it's perhaps the best to give reasonable default values in the source code with annotations and use the XML descriptor only where really necessary to override parameters or give additional ones to the container. This is pretty much the same Ulf already told you for the web.xml descriptor of JSPs and servlets.

Marco
[ May 28, 2008: Message edited by: Marco Ehrentreich ]
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Thank you Ulf and Marco.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Annotation guide