自己建立註解,和使用註解

2021-06-27 01:01:41 字數 602 閱讀 7940

首先自己建立註解的類

mytest:

@retention(value=retentionpolicy.runtime)    //在執行的時候擦除    @retention表示執行時候的範圍,一共有3種

@target(value=)       //這個表明是要用到哪個上

public @inte***ce mytest

自己在建立乙個,然後使用自己創造的mytest註解

runtest類:

public class runtest

@mytest

@test

public void abc()

@mytest

@test

private void aaa()

}這裡說明下,如果執行的話是不會執行aaa這個方法的,因為它為private,如果要通過那麼要在test裡面設定乙個方法setaccessible(true)為true

所以如果要執行將它注釋掉

現在開始編譯讓它把private也可以執行起來,建立乙個myunit

public class myunit

m.invoke(obj);}}

}}

註解和反射使用

作用 註解是附加在 中的一些資訊,用於編譯 執行時解析和使用,起到說明配置作用 主要學習兩方面1 元註解,2 自定義註解 一 元註解 包括 1.target,2.retention 3.documented,4.inherited 幾種 1 target 作用 用於描述註解的使用範圍 即 註解是描述...

Spring註解方式和註解小結

用於建立物件的4種註解方法 component 註解 beans xmlns xmlns xsi xmlns context xsi schemalocation spring beans.xsd spring context.xsd 在其實現類上新增註解 component userservice...

TestNG註解 方法和類註解

本文介紹 beforemethod aftermethod beforeclass afterclass四個註解 beforemethod 在測試類中的每乙個 test註解方法執行之前執行一次 aftermethod 在測試類中的每乙個 test註解方法執行之後執行一次 beforeclass 在測...