Java中的@interface注解使用詳解
1. 引言
注解@interface不是接口是注解類,在jdk1.5之后加入的功能,使用@interface自定義注解時,自動繼承了java.lang.annotation.Annotation接口。
在定義注解時,不能繼承其他的注解或接口。@interface用來聲明一個注解,其中的每一個方法實際上是聲明了一個配置參數(shù)。方法的名稱就是參數(shù)的名稱,返回值類型就是參數(shù)的類型(返回值類型只能是基本類型、Class、String、enum)。可以通過default來聲明參數(shù)的默認(rèn)值。
就是不能定義 public @interface A extends B 這樣的形式,必須裸的,只能是 public @interface A 形式
在Java API文檔中特意強(qiáng)調(diào)了如下內(nèi)容: Annotation是所有注釋類型的公共擴(kuò)展接口。注意,手動擴(kuò)展這個接口并不定義注釋類型。還要注意,這個接口本身并不定義注釋類型。注釋類型的更多信息可以在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中的參數(shù)ElementType
該參數(shù)聲明這個注解可以加在上面位置,只能在類上、或只能在方法上
@component注解為例,只能加在類或接口上,不能加載方法上。
public enum ElementType {
TYPE, /* 類、接口(包括注釋類型)或枚舉聲明 */
FIELD, /* 字段聲明(包括枚舉常量) */
METHOD, /* 方法聲明 */
PARAMETER, /* 參數(shù)聲明 */
CONSTRUCTOR, /* 構(gòu)造方法聲明 */
LOCAL_VARIABLE, /* 局部變量聲明 */
ANNOTATION_TYPE, /* 注釋類型聲明 */
PACKAGE /* 包聲明 */
}2.3 @Retention中的參數(shù)RetentionPolicy
public enum RetentionPolicy {
SOURCE, /* Annotation信息僅存在于編譯器處理期間,編譯器處理完之后就沒有該Annotation信息了 */
CLASS, /* 編譯器將Annotation存儲于類對應(yīng)的.class文件中。默認(rèn)行為 */
RUNTIME /* 編譯器將Annotation存儲于class文件中,并且可由JVM讀入 */
}2.4 成員變量
以無形參的方法形式來聲明Annotation的成員變量,方法名和返回值定義了成員變量名稱和類型。
注意:定義成員變量,以方法的形式定義,而不是普通類的那種直接定義方式
使用default關(guān)鍵字設(shè)置默認(rèn)值,沒設(shè)置默認(rèn)值的變量則使用時必須提供,有默認(rèn)值的變量使用時可以設(shè)置也可以不設(shè)置。
//定義帶成員變量注解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類實現(xiàn)了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元素唯一的特點來實現(xiàn)遞歸搜索所有的注解
if (set.add(annotation)) {
//獲取注解的注解數(shù)組
if (annotation.annotationType().getAnnotations().length > 0) {
//搜索子注解
for (Annotation childAnnotation : annotation.annotationType().getAnnotations()) {
recursivelyAnnotaion(childAnnotation, set);
}
}
System.out.println(annotation.annotationType().getName());
}
}
}執(zhí)行結(jié)果:
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;
// 獲取包的名字 并進(jìn)行替換
String packageName = pack;
String packageDirName = packageName.replace('.', '/');
// 定義一個枚舉的集合 并進(jìn)行循環(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();
// 如果是以文件的形式保存在服務(wù)器上
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();
// 同樣的進(jìn)行循環(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('/');
// 如果以"/"結(jié)尾 是一個包
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結(jié)尾的文件(編譯好的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));
// 經(jīng)過回復(fù)同學(xué)的提醒,這里用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<?>>();
// 獲取指定接口的實現(xiàn)類
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)) {// 自身并不加進(jìn)去
classes.add(cls);
} else {
}
}
}
} catch (Exception e) {
System.out.println("出現(xiàn)異常");
}
}
return classes;
}
}4. 注解的作用
Annotation 是一個輔助類,它在 Junit、Struts、Spring 等工具框架中被廣泛使用。
4.1 編譯檢查
Annotation 具有"讓編譯器進(jìn)行編譯檢查的作用"。例如,@SuppressWarnings, @Deprecated 和 @Override 都具有編譯檢查作用。
4.2 在反射中使用 Annotation
在reflect庫中的的Class, Method, Field 等類都實現(xiàn)了AnnotatedElement接口,這也意味著,我們可以在反射中解析并使用 Annotation,詳細(xì)是使用方法可以參考第三節(jié)中的內(nèi)容。
4.3 根據(jù) Annotation 生成幫助文檔
通過給 Annotation 注解加上 @Documented 標(biāo)簽,能使該 Annotation 標(biāo)簽出現(xiàn)在 javadoc 中。
到此這篇關(guān)于Java中的@interface注解使用詳解的文章就介紹到這了,更多相關(guān)@interface注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot+redis 監(jiān)聽過期Key的操作方法
這篇文章主要介紹了spring boot+redis 監(jiān)聽過期Key,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
SpringBoot使用除了Jasypt對YML文件配置內(nèi)容進(jìn)行加密方式
文章介紹了如何在Java項目中使用Jasypt進(jìn)行加密和解密,包括在pom.xml中添加依賴、在application.yml中配置信息、創(chuàng)建解密Bean以及確保秘鑰和算法的一致性2025-11-11
mybatis if test 不為空字符串或null的解決
這篇文章主要介紹了mybatis if test 不為空字符串或null的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
java根據(jù)不同的參數(shù)調(diào)用不同的實現(xiàn)類操作
這篇文章主要介紹了java根據(jù)不同的參數(shù)調(diào)用不同的實現(xiàn)類操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

