見出し画像

@Priorityアノテーションを読んでみた


javax.annotation.Priorityアノテーションを読んでみました。
ドキュメントはこちら

Priorityアノテーションは、実行順序を指定するためのアノテーションです。
優先順位を指定するために数値をもたせます。
数値が大きければ大きいほど優先度が高く、先に処理されます。

使い方はこんな感じです。

@Priority(100)

では早速読んでいきます。


コピーライトについて

 * Copyright (c) 2005, 2018 Oracle and/or its affiliates. All rights reserved.
【ぐぐるさん訳】
Copyright(c)2005、2018Oracleおよび/またはその関連会社。全著作権所有。

直訳の通りなので割愛。


パッケージ名とインポートしているパッケージについて

package javax.annotation;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
【ぐぐるさん訳】
名称なので割愛。

この@Priorityアノテーションは、javax.annotationパッケージに属しています。

java.lang.annotationパッケージ配下のすべてのパッケージ、
java.lang.annotation.ElementTypeパッケージ配下のすべてのパッケージ、
java.lang.annotation.RetentionPolicyパッケージ配下のすべてのパッケージを
インポートしています。


Priorityクラスの使い方について

/**
* The <code>Priority</code> annotation can be applied to classes 
* or parameters to indicate in what order they should be used.  
* The effect of using the <code>Priority</code> annotation in
* any particular instance is defined by other specifications that 
* define the use of a specific class.
【ぐぐるさん訳】
Priorityアノテーションをクラスまたはパラメーターに適用して、使用する順序を示すことができます。
特定のインスタンスでPriorityアノテーションを使用する効果は、特定のクラスの使用を定義する他の仕様によって定義されます。

Priorityアノテーションはクラスかパラメータに使えます。

Priorityアノテーションを使うと、
各クラスの実行順序を指定できるらしいです。

Priorityアノテーションの効果は、同じクラス同士に適用されるらしいです。

* <p>
* For example, the Interceptors specification defines the use of
* priorities on interceptors to control the order in which
* interceptors are called.</p>
【ぐぐるさん訳】
たとえば、インターセプター仕様では、インターセプターの優先順位を使用して、インターセプターが呼び出される順序を制御することを定義しています。

たとえば、インターセプターだったら
同じインターセプター同士の間で実行順序を指定できます。


* <p>
* Priority values should generally be non-negative, with negative values
* reserved for special meanings such as "undefined" or "not specified".
* A specification that defines use of the <code>Priority</code> annotation may define
* the range of allowed priorities and any priority values with special
* meaning.</p>
【ぐぐるさん訳】
優先度の値は通常、負ではなく、負の値は「未定義」や「指定なし」などの特別な意味のために予約されている必要があります。 Priorityアノテーションの使用を定義する仕様では、許可される優先度の範囲と、特別な意味を持つ優先度の値を定義できます。

優先順位を指定するための数値は基本マイナスになることはないです。
「未定義」とか「指定なし」とかって言いたいときに、マイナスの数値を使います。

Priorityアノテーションの使い方を指定してる仕様を別で作れば、
優先順序の範囲とか、特別な意味のある値とか定義できるっぽい。


* @since Common Annotations 1.2
*/
【ぐぐるさん訳】
Common Annotations1.2以降

Common Annotationsのバージョン1.2から導入されたらしいです。


ついてるアノテーション

@Target({TYPE,PARAMETER})
@Retention(RUNTIME)
@Documented
【ぐぐるさん訳】
割愛。

クラス、パラメータにしか使えないみたいです。
このクラスは実行中ずっと使えます。
JavaDocにアノテーション表示します。


クラスのもってる値、メソッド

public @interface Priority {
   /**
    * The priority value.
    */
   int value();
}
【ぐぐるさん訳】
割愛。

Prorityっていうアノテーションです。
優先順位の値を整数型でもってます。


あんまりいけてないやり方なのかもしれませんが、
お仕事で@ControllerAdviceクラスに順番をつけなきゃいけない場面があったので、その時に使いました。


以下、全文です。

 * Copyright (c) 2005, 2018 Oracle and/or its affiliates. All rights reserved.

package javax.annotation;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

/**
* The <code>Priority</code> annotation can be applied to classes 
* or parameters to indicate in what order they should be used.  
* The effect of using the <code>Priority</code> annotation in
* any particular instance is defined by other specifications that 
* define the use of a specific class.
* <p>
* For example, the Interceptors specification defines the use of
* priorities on interceptors to control the order in which
* interceptors are called.</p>
* <p>
* Priority values should generally be non-negative, with negative values
* reserved for special meanings such as "undefined" or "not specified".
* A specification that defines use of the <code>Priority</code> annotation may define
* the range of allowed priorities and any priority values with special
* meaning.</p>
*
* @since Common Annotations 1.2
*/
@Target({TYPE,PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface Priority {
   /**
    * The priority value.
    */
   int value();
}


この記事が気に入ったらサポートをしてみませんか?