这些 Java 注释有什么功能? [英] What function do these Java annotations serve?

查看:27
本文介绍了这些 Java 注释有什么功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然不清楚Java中注释的目的.最初我认为它们只是作为文档.但是从 Google App Engine Datastore 中查看此文档,我不太确定.@PersistenceCapable(identityType = IdentityType.APPLICATION) 看起来更像是一个方法签名.

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 Datastore, I'm not so sure. @PersistenceCapable(identityType = IdentityType.APPLICATION) looks more like a method signature.

这种注解的目的是什么?它有什么作用?

What's the purpose of this type of annotation? What does it do?

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private String firstName;

    @Persistent
    private String lastName;

    @Persistent
    private Date hireDate;

    public Employee(String firstName, String lastName, Date hireDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.hireDate = hireDate;
    }

    // Accessors for the fields.  JDO doesn't use these, but your application does.

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    } 

    // ... other accessors...
}

推荐答案

它们是源级元数据.它们是一种向代码中添加信息的方式,这种方式很容易被机器处理.

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.

在您的示例中,它们用于为该实体类型配置对象关系映射.例如,id 字段应该是该对象的主键,firstName、lastName 和hiringDate 应该存储在数据库中.(将这些字段与某些瞬态对象状态区分开来.)

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.)

对 JDO 的 GAE 支持需要知道您将尝试在数据库中存储哪些对象.它通过查看代码中的类,查找使用 @PersistenceCapable 注释的类来实现这一点.

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.

通常,它们用于替换您之前使用外部配置文件的位置;Java 标准库具有读取代码中注释的工具,这使得它们比滚动您自己的配置文件管道更容易处理,并且免费为您提供 IDE 支持.

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.

这篇关于这些 Java 注释有什么功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆