What function do these Java annotations serve?

后端 未结 3 1747
臣服心动
臣服心动 2021-01-05 12:45

I\'m still not clear on the purpose of annotations in Java. Initially I thought they just served as documentation. But looking at this documentation from Google App Engine D

相关标签:
3条回答
  • 2021-01-05 12:50

    Annotations can be processed with the Annotation Processing Tool APIs to auto-generate boilerplate code.

    0 讨论(0)
  • 2021-01-05 12:52

    I think that these come from the Java Data Objects API. It's an API that overlaps to a degree with what EJB3 is supposed to accomplish. Same concepts, different syntax and tools.

    If you are not familiar with annotations in general, check the Java tutorial.

    0 讨论(0)
  • 2021-01-05 12:53

    They're source-level metadata. They're a way of adding information to the code that's not code, and that is easily machine-processed.

    In your example, they're used to configure object-relational mapping for that entity type. It's saying that for example the id field should be the primary key for that object, and that firstName, lastName, and hireDate should be stored in the database. (To tell those fields apart from some transient object state.)

    The GAE support for JDO needs to know what objects you'll try to store in the database. It does this by looking through the classes in your code, looking for the ones that are annotated with @PersistenceCapable.

    Commonly, they're used to replace where you'd use external configuration files before; the Java standard library has tools to read the annotations in your code, which makes them much easier to process than rolling your own configuration file plumbing, and gets you IDE support for free.

    0 讨论(0)
提交回复
热议问题