这些Java注解起到什么功能

这些Java注解起到什么功能

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

问题描述

我还没有在Java注释的目的明确。起初我还以为他们只是充当文档。但是看着从 , 我不确定。 @PersistenceCapable(identityType = IdentityType.APPLICATION)看起来更像是一个方法签名。

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

 进口java.util.Date;
进口javax.jdo.annotations.IdGeneratorStrategy;
进口javax.jdo.annotations.IdentityType;
进口javax.jdo.annotations.PersistenceCapable;
进口javax.jdo.annotations.Persistent;
进口javax.jdo.annotations.PrimaryKey;@PersistenceCapable(identityType = IdentityType.APPLICATION)
公共类Employee {
    @首要的关键
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    私人长期身份证;    @Persistent
    私有String的firstName;    @Persistent
    私人字符串的lastName;    @Persistent
    私人日期雇用日期;    公务员(字符串名字,字符串姓氏,日期雇佣日期){
        this.firstName =名字;
        this.lastName = lastName的;
        this.hireDate =雇用日期;
    }    //访问器领域。 JDO不使用这些,但应用程序。    众长的getId(){
        返回ID;
    }    公共字符串的getFirstName(){
        返回的firstName;
    }    // ...其他访问者...
}


解决方案

他们是源代码级的元数据。他们将信息添加到code,这不是code的一种方式,那就是很容易机加工。

在你的榜样,他们已经习惯了配置该实体类型的对象关系映射。它说例如id字段应该是该对象的主键,该名字,姓氏,并雇佣日期应存储在数据库中。 (从某些瞬态对象状态分辨这些字段。)

有关JDO的GAE支持需要知道哪些对象,你会尝试在数据库中存储。它通过寻找通过在code类,寻找那些标注有@PersistenceCapable的那些这样做了。

一般情况下,他们已经习惯了在这里代替你之前使用外部配置文件; Java标准库有工具来阅读你的code,这使得它们更容易比滚动自己的配置文件的管道来处理注释,并让你免费的IDE支持。

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.

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.

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

08-19 13:30