• Post Reply Bookmark Topic Watch Topic
  • New Topic

How deoes annotations work?  RSS feed

 
Shankara Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can some when explains me how annotations work at run time and how they are useful?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the tutorial: Lesson: Annotations
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read this?

EDIT: Ninja'd by 27 seconds... (my record of being ninja'd is 1 s )
 
Shankara Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.. I have read them. But i haven't got any idea like where they can be useful. I have seen annotations are used in hibernate, spring API etc. How these frameworks uses annotations?
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the most part, the java compiler puts the runtime annotations in the byte code of the class. Also the reflection API provides methods that let you introspect classes for annotations. By itself runtime annotations are purely informational. it is a way for a creator of a class to provide some metadata of the class to other parts of the code. Thats it.

Runtime Annotations become powerful is when frameworks mix the runtime annotations with other technologies. Everyone does it a bit differently.

For example, hibernate scans your class path for classes annottated with hibernate/Jpa annotations. It uses that to build the internal object representation of the database. For example. When it finds entity annotation, it introspect s the class the find it's properties, and from that it builds a picture of what the database should look like. Also. It finds the named queries and then uses this internal picture to convert the jql to SQL.

Spring, OTH, chiefly uses annotations for auto wiring, aop and controlling object life cycle. So, when instantiates a class it checks it for all the annotations that it has. If it has the annotations that it supports, it will invoke the correct behavior. For example if it finds auto wiring annotation, it will do the auto wiring. If it finds an annotated method that needs code weaving, it will weave the code in. And so on and so forth
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations can be put to good use with JEE: servlets, EJB's, web services, timers etc. can be "driven" through them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!