Java中的@interface注解使用詳解
1. 引言
注解@interface不是接口是注解類,在jdk1.5之后加入的功能,使用@interface自定義注解時,自動繼承了java.lang.annotation.Annotation接口。
在定義注解時,不能繼承其他的注解或接口。@interface用來聲明一個注解,其中的每一個方法實際上是聲明了一個配置參數。方法的名稱就是參數的名稱,返回值類型就是參數的類型(返回值類型只能是基本類型、Class、String、enum)??梢酝ㄟ^default來聲明參數的默認值。
就是不能定義 public @interface A extends B 這樣的形式,必須裸的,只能是 public @interface A 形式
在Java API文檔中特意強調了如下內容: Annotation是所有注釋類型的公共擴展接口。注意,手動擴展這個接口并不定義注釋類型。還要注意,這個接口本身并不定義注釋類型。注釋類型的更多信息可以在Java™語言規(guī)范的9.6節(jié)。
2. 語法規(guī)范
我們以spring中的@component注解為例,其格式如下所示
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Indexed public @interface Component { String value() default ""; }
其中主要包括四個部分
2.1 繼承的Annotation父接口
等價于,編譯時會自動添加這個父接口:
public @interface Component extends java.lang.annotation.Annotation{ }
2.2 @Target中的參數ElementType
該參數聲明這個注解可以加在上面位置,只能在類上、或只能在方法上
@component注解為例,只能加在類或接口上,不能加載方法上。
public enum ElementType { TYPE, /* 類、接口(包括注釋類型)或枚舉聲明 */ FIELD, /* 字段聲明(包括枚舉常量) */ METHOD, /* 方法聲明 */ PARAMETER, /* 參數聲明 */ CONSTRUCTOR, /* 構造方法聲明 */ LOCAL_VARIABLE, /* 局部變量聲明 */ ANNOTATION_TYPE, /* 注釋類型聲明 */ PACKAGE /* 包聲明 */ }
2.3 @Retention中的參數RetentionPolicy
public enum RetentionPolicy { SOURCE, /* Annotation信息僅存在于編譯器處理期間,編譯器處理完之后就沒有該Annotation信息了 */ CLASS, /* 編譯器將Annotation存儲于類對應的.class文件中。默認行為 */ RUNTIME /* 編譯器將Annotation存儲于class文件中,并且可由JVM讀入 */ }
2.4 成員變量
以無形參的方法形式來聲明Annotation的成員變量,方法名和返回值定義了成員變量名稱和類型。
注意:定義成員變量,以方法的形式定義,而不是普通類的那種直接定義方式
使用default關鍵字設置默認值,沒設置默認值的變量則使用時必須提供,有默認值的變量使用時可以設置也可以不設置。
//定義帶成員變量注解MyTag @Rentention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyTag{ //定義兩個成員變量,以方法的形式定義 String name(); int age() default 20; }
在使用 該注解的地方,需要給屬性賦值,如果屬性定義時,帶有default,則可以不用賦值,age可以不用賦值,而name必須賦值:
//使用 public class Test{ @MyTag(name="test") public void info(){} }
3. 獲取注解信息
可以通過反射的方式獲取注解在某個元素上的注解和其中的值,在Java反射類庫reflect中,Class類實現了AnnotatedElement接口,其中包含多個獲取annotation的方法
package bat.ke.qq.com.controller; import org.springframework.stereotype.Component; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @Component public class MySearchMetaAnnations { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { List<String> test = new ArrayList<>(); //通過反射獲取某個類上注解 Annotation[] annotations = MySearchMetaAnnations.class.getAnnotations(); Set<Annotation> set = new HashSet<>(); for (Annotation annotation : annotations) { recursivelyAnnotaion(annotation, set); } System.out.println(set); } //遞歸獲取注解的注解 public static void recursivelyAnnotaion(Annotation annotation, Set<Annotation> set) { //通過set元素唯一的特點來實現遞歸搜索所有的注解 if (set.add(annotation)) { //獲取注解的注解數組 if (annotation.annotationType().getAnnotations().length > 0) { //搜索子注解 for (Annotation childAnnotation : annotation.annotationType().getAnnotations()) { recursivelyAnnotaion(childAnnotation, set); } } System.out.println(annotation.annotationType().getName()); } } }
執(zhí)行結果:
java.lang.annotation.Target
java.lang.annotation.Retention
java.lang.annotation.Documented
java.lang.annotation.Target
org.springframework.stereotype.Indexed
org.springframework.stereotype.Component
[@java.lang.annotation.Documented(), @org.springframework.stereotype.Indexed(), @java.lang.annotation.Target(value=[TYPE]), @org.springframework.stereotype.Component(value=), @java.lang.annotation.Target(value=[ANNOTATION_TYPE]), @java.lang.annotation.Retention(value=RUNTIME)]
使用場景——框架初始化過程中模擬掃描jar包和文件夾中的所有注解
模擬加載某個jar包后,掃描里面的類及注解
/** * 一個簡單的模擬搜索包下面所有注解的工具類 */ public class MySearchAnnationsInPackage { public static void main(String[] args) { // 包下面的類 Set<Class<?>> clazzs = getClasses("designPattern"); if (clazzs == null) { return; } System.out.println( "class總量:" + clazzs.size()); // 某類或者接口的子類 //Set<Class<?>> inInterface = getByInterface(Object.class, clazzs); //System.out.printf(inInterface.size() + ""); for (Class<?> clazz : clazzs) { // 獲取類上的注解 Annotation[] annos = clazz.getAnnotations(); for (Annotation anno : annos) { System.out.println(clazz.getSimpleName().concat(".").concat(anno.annotationType().getSimpleName())); } // 獲取方法上的注解 Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { Annotation[] annotations = method.getDeclaredAnnotations(); for (Annotation annotation : annotations) { System.out.println(clazz.getSimpleName().concat(".").concat(method.getName()).concat(".") .concat(annotation.annotationType().getSimpleName())); } } } } /** * 從包package中獲取所有的Class * * @param pack * @return */ public static Set<Class<?>> getClasses(String pack) { // 第一個class類的集合 Set<Class<?>> classes = new LinkedHashSet<>(); // 是否循環(huán)迭代 boolean recursive = true; // 獲取包的名字 并進行替換 String packageName = pack; String packageDirName = packageName.replace('.', '/'); // 定義一個枚舉的集合 并進行循環(huán)來處理這個目錄下的things Enumeration<URL> dirs; try { dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); // 循環(huán)迭代下去 while (dirs.hasMoreElements()) { // 獲取下一個元素 URL url = dirs.nextElement(); // 得到協(xié)議的名稱 String protocol = url.getProtocol(); // 如果是以文件的形式保存在服務器上 if ("file".equals(protocol)) { System.err.println("file類型的掃描"); // 獲取包的物理路徑 String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); // 以文件的方式掃描整個包下的文件 并添加到集合中 findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); } else if ("jar".equals(protocol)) { // 如果是jar包文件 // 定義一個JarFile // System.err.println("jar類型的掃描"); JarFile jar; try { // 獲取jar jar = ((JarURLConnection) url.openConnection()).getJarFile(); // 從此jar包 得到一個枚舉類 Enumeration<JarEntry> entries = jar.entries(); // 同樣的進行循環(huán)迭代 while (entries.hasMoreElements()) { // 獲取jar里的一個實體 可以是目錄 和一些jar包里的其他文件 如META-INF等文件 JarEntry entry = entries.nextElement(); String name = entry.getName(); // 如果是以/開頭的 if (name.charAt(0) == '/') { // 獲取后面的字符串 name = name.substring(1); } // 如果前半部分和定義的包名相同 if (name.startsWith(packageDirName)) { int idx = name.lastIndexOf('/'); // 如果以"/"結尾 是一個包 if (idx != -1) { // 獲取包名 把"/"替換成"." packageName = name.substring(0, idx).replace('/', '.'); } // 如果可以迭代下去 并且是一個包 if ((idx != -1) || recursive) { // 如果是一個.class文件 而且不是目錄 if (name.endsWith(".class") && !entry.isDirectory()) { // 去掉后面的".class" 獲取真正的類名 String className = name.substring(packageName.length() + 1, name.length() - 6); try { // 添加到classes classes.add(Class.forName(packageName + '.' + className)); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } } } catch (IOException e) { // log.error("在掃描用戶定義視圖時從jar包獲取文件出錯"); e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } return classes; } /** * 以文件的形式來獲取包下的所有Class * * @param packageName * @param packagePath * @param recursive * @param classes */ public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set<Class<?>> classes) { // 獲取此包的目錄 建立一個File File dir = new File(packagePath); // 如果不存在或者 也不是目錄就直接返回 if (!dir.exists() || !dir.isDirectory()) { // log.warn("用戶定義包名 " + packageName + " 下沒有任何文件"); return; } // 如果存在 就獲取包下的所有文件 包括目錄 File[] dirfiles = dir.listFiles(new FileFilter() { // 自定義過濾規(guī)則 如果可以循環(huán)(包含子目錄) 或則是以.class結尾的文件(編譯好的java類文件) public boolean accept(File file) { return (recursive && file.isDirectory()) || (file.getName().endsWith(".class")); } }); // 循環(huán)所有文件 for (File file : dirfiles) { // 如果是目錄 則繼續(xù)掃描 if (file.isDirectory()) { findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes); } else { // 如果是java類文件 去掉后面的.class 只留下類名 String className = file.getName().substring(0, file.getName().length() - 6); try { // 添加到集合中去 // classes.add(Class.forName(packageName + '.' + className)); // 經過回復同學的提醒,這里用forName有一些不好,會觸發(fā)static方法,沒有使用classLoader的load干凈 classes.add( Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className)); } catch (ClassNotFoundException e) { // log.error("添加用戶自定義視圖類錯誤 找不到此類的.class文件"); e.printStackTrace(); } } } } @SuppressWarnings({"rawtypes", "unchecked"}) public static Set<Class<?>> getByInterface(Class clazz, Set<Class<?>> classesAll) { Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); // 獲取指定接口的實現類 if (!clazz.isInterface()) { try { /** * 循環(huán)判斷路徑下的所有類是否繼承了指定類 并且排除父類自己 */ Iterator<Class<?>> iterator = classesAll.iterator(); while (iterator.hasNext()) { Class<?> cls = iterator.next(); /** * isAssignableFrom該方法的解析,請參考博客: * http://blog.csdn.net/u010156024/article/details/44875195 */ if (clazz.isAssignableFrom(cls)) { if (!clazz.equals(cls)) {// 自身并不加進去 classes.add(cls); } else { } } } } catch (Exception e) { System.out.println("出現異常"); } } return classes; } }
4. 注解的作用
Annotation 是一個輔助類,它在 Junit、Struts、Spring 等工具框架中被廣泛使用。
4.1 編譯檢查
Annotation 具有"讓編譯器進行編譯檢查的作用"。例如,@SuppressWarnings, @Deprecated 和 @Override 都具有編譯檢查作用。
4.2 在反射中使用 Annotation
在reflect庫中的的Class, Method, Field 等類都實現了AnnotatedElement接口,這也意味著,我們可以在反射中解析并使用 Annotation,詳細是使用方法可以參考第三節(jié)中的內容。
4.3 根據 Annotation 生成幫助文檔
通過給 Annotation 注解加上 @Documented 標簽,能使該 Annotation 標簽出現在 javadoc 中。
到此這篇關于Java中的@interface注解使用詳解的文章就介紹到這了,更多相關@interface注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
maven項目test執(zhí)行main找不到資源文件的問題及解決
這篇文章主要介紹了maven項目test執(zhí)行main找不到資源文件的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring Boot使用Redisson實現滑動窗口限流的項目實踐
滑動窗口限流是一種流量控制策略,用于控制在一定時間內的請求頻率,本文主要介紹了Spring Boot使用Redisson實現滑動窗口限流的項目實踐,具有一定的參考價值,感興趣的可以了解一下2024-03-03java面試突擊之sleep和wait有什么區(qū)別詳析
按理來說sleep和wait本身就是八竿子打不著的兩個東西,但是在實際使用中大家都喜歡拿他們來做比較,或許是因為它們都可以讓線程處于阻塞狀態(tài),這篇文章主要給大家介紹了關于java面試突擊之sleep和wait有什么區(qū)別的相關資料,需要的朋友可以參考下2022-02-02Springboot優(yōu)化內置服務器Tomcat優(yōu)化方式(underTow)
本文詳細介紹了Spring Boot中Tomcat和Undertow服務器的配置和優(yōu)化,包括初始線程數、最大線程數、最小備用線程數、最大請求數等參數的優(yōu)化建議,以及在高并發(fā)場景下Undertow相對于Tomcat的優(yōu)勢2024-12-12Springboot-Starter造輪子之自動鎖組件lock-starter實現
這篇文章主要為大家介紹了Springboot-Starter造輪子之自動鎖組件lock-starter實現詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05背包問題-動態(tài)規(guī)劃java實現的分析與代碼
這篇文章主要給大家介紹了關于背包問題動態(tài)規(guī)劃java實現的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12