Repeatable與重複註解的使用

2021-10-11 07:28:21 字數 1696 閱讀 3267

@repeatable是jdk1.8出現的元註解,在早前的版本中宣告的註解annotation是不能夠重複作用在同乙個類或方法上的,而有些時候我們的開發偏偏需要這樣的使用訴求和場景。

單一註解的使用方式如下:

/**

* created by guanjian on 2020/11/30 19:58

*/@documented

@retention

(retentionpolicy.runtime)

@target

(elementtype.method)

public @inte***ce

aop

引入@repeatable註解後,改造如下:

/**

* created by guanjian on 2020/11/30 19:58

*/@documented

@retention

(retentionpolicy.runtime)

@target

(elementtype.method)

@repeatable

(aops.

class

)//這裡作用在業務註解上

public @inte***ce

aop

引入了@aops返回@aop陣列以支援@aop多次註解重複使用

/**

* created by guanjian on 2020/11/30 19:58

*/@documented

@retention

(retentionpolicy.runtime)

@target

(elementtype.method)

public @inte***ce

aops

/**

* created by guanjian on 2020/11/30 20:00

*/@aspect

@component

("aopaspect"

)public

class

aopaspect);

return pjp.

proceed()

;}protected method getmethod

(proceedingjoinpoint pjp)

catch

(nosuchmethodexception e)

return method;

}}

/**

* created by guanjian on 2020/11/30 20:13

*/@component

("aopservice"

)public

class

aopservice

}

關於多個註解的使用總結如下:

攔截註解

使用註解個數

getannotation

getannotationsbytype

@aop單個√

√@aop多個×

×@aops單個×

×@aops多個×

√@aop + @aops單個√

√@aop + @aops多個×

註解概念與常用註解

註解概念與常用註解 註解在開發過程中可以簡化複雜的檔案配置,優化設計模式結構,使得現在的開發模式逐漸變為註解 設計模式 反射 annotation概念 註解在 中是一種標記符號,這些標記符號可以在 的原始碼期 編譯期和執行期被讀取,執行相應的功能。annation 修飾範圍 註解可以用來修飾 的包 ...

Java8 重複註解和型別註解

重複註解 可以在乙個類上或方法上重複多次使用的註解 示例1 自定義可重複註解 使用 repeatable元註解,引數為可重複註解的容器 repeatable myannotations.class target retention retentionpolicy.runtime public int...

struts2 註解 防止表單重複提交

註解 在struts2中使用註解,主要完成對action的定義。但諸如常量定義 定義等還需要在struts.xml中完成。需要引入struts2 convention plugin jar action基本註解示例 namespace value test 對於xml中的namespace test...