JAVA自定義注解
注解概念
注解是Java SE 5.0版本開始引入的概念,它是對java源代碼的說明,是一種元資料(描述資料的資料),
注解和注釋的不同
-
注釋
注釋是對代碼的說明,給代碼的讀者看,便于幫讀者梳理業務邏輯;在程式代碼中經常看到的以@ 開頭的大部分是注解;
-
注解
注解也是對代碼的說明,需要配合工具(決議它的代碼)使用,參與代碼的編譯,給應用程式看的;
注解分類
注解以@開頭,我們會在應用程式中見到各種各樣的注解,比如 @Autowired,@Service,@Controller,@Override ,@Test,@Value 等等,按照來源劃分,可以分為 JDK的注解,第三方的注解,自定義注解,
JDK注解
JAVA 內置注解
- @Override (標記重寫方法)
- @Deprecated (標記過時)
- @SuppressWarnings (忽略警告)
元注解 (注解的注解)
- @Target (注解的作用目標)
- @Retention (注解的生命周期)
- @Document (注解是否被包含在JavaDoc中)
- @Inherited (是否允許子類集成該注解)
第三方注解(各種框架注解)
請自行百度各個框架的注解詳解
自定義注解
使用元注解自己定義的注解
JDK中有一些元注解,主要有@Target,@Retention,@Document,@Inherited用來修飾注解,
@Target
表明該注解可以應用的java元素型別
| Target型別 | 描述 |
|---|---|
| ElementType.TYPE | 應用于類、介面(包括注解型別)、列舉 |
| ElementType.FIELD | 應用于屬性(包括列舉中的常量) |
| ElementType.METHOD | 應用于方法 |
| ElementType.PARAMETER | 應用于方法的形參 |
| ElementType.CONSTRUCTOR | 應用于建構式 |
| ElementType.LOCAL_VARIABLE | 應用于區域變數 |
| ElementType.ANNOTATION_TYPE | 應用于注解型別 |
| ElementType.PACKAGE | 應用于包 |
| ElementType.TYPE_PARAMETER | 1.8版本新增,應用于型別變數) |
| ElementType.TYPE_USE | 1.8版本新增,應用于任何使用型別的陳述句中(例如宣告陳述句、泛型和強制轉換陳述句中的型別) |
@Retention
表明該注解的生命周期
| 生命周期型別 | 描述 |
|---|---|
| RetentionPolicy.SOURCE | 編譯時被丟棄,不包含在類檔案中 |
| RetentionPolicy.CLASS | JVM加載時被丟棄,包含在類檔案中,默認值 |
| RetentionPolicy.RUNTIME | 由JVM 加載,包含在類檔案中,在運行時可以被獲取到 |
@Document
表明該注解標記的元素可以被Javadoc 或類似的工具檔案化
@Inherited
表明使用了@Inherited注解的注解,所標記的類的子類也會擁有這個注解
注解格式
/**
* 修飾符 @interface 注解名 {
* 注解元素的宣告1
* 注解元素的宣告2
* }
* 修飾符:訪問修飾符必須為public,不寫默認為pubic;
* 關鍵字:必須為@interface;
* 注解名: 注解名稱為自定義注解的名稱,使用時還會用到;
* 注解型別元素:注解型別元素是注解中內容,可以理解成自定義介面的實作部分;
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
/**
* 注解的元素宣告的兩種形式
* type elementName();
* type elementName() default value;
*/
String value() default "test";
}
注解樣例
接下來我們以Spring中的 @Service 注解為例
@Target({ElementType.TYPE})// ElementType.TYPE 代表在注解上使用
@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表運行時使用,可以通過反射獲取到
@Documented//包含在JavaDoc中
@Component//允許通過包掃描的方式自動檢測
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
@Annotation
JDK1.5有的,在rt.jar包下 java.lang.annotation包下,所有的注解默認繼承了Annotation介面,但是它本身不能定義注解,
package java.lang.annotation;
/**
* 所有的注解默認繼承了Annotation介面,但是它本身不能定義注解,
* The common interface extended by all annotation types. Note that an
* interface that manually extends this one does <i>not</i> define
* an annotation type. Also note that this interface does not itself
* define an annotation type.
*
* More information about annotation types can be found in section 9.6 of
* <cite>The Java™ Language Specification</cite>.
*
* The {@link java.lang.reflect.AnnotatedElement} interface discusses
* compatibility concerns when evolving an annotation type from being
* non-repeatable to being repeatable.
*
* @author Josh Bloch
* @since 1.5
*/
public interface Annotation {
.
.
.
}
實作自定義注解
第一步-定義自定義注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
String value() default "test";
}
第二步-配置注解
@Data
@Builder
@MyTestAnnotation
public class MyBean {
private String name;
private int age;
}
第三步-利用反射決議注解
public class MyTest {
//isAnnotationPresent:判斷當前元素是否被指定注解修飾
//getAnnotation:回傳指定的注解
//getAnnotations:回傳所有的注解
public static void main(String[] args) {
try {
//獲取MyBean的Class物件
MyBean myBean = MyBean.builder().build();
Class clazz = myBean.getClass();
//判斷myBean物件上是否有MyTestAnnotation注解
if (clazz.isAnnotationPresent(MyTestAnnotation.class)) {
System.out.println("MyBean類上配置了MyTestAnnotation注解!");
//獲取該物件上MyTestAnnotation型別的注解
MyTestAnnotation myTestAnnotation = (MyTestAnnotation) clazz.getAnnotation(MyTestAnnotation.class);
System.out.println(myTestAnnotation.value());
} else {
System.out.println("MyBean類上沒有配置MyTestAnnotation注解!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
執行main方法,運行結果:
Connected to the target VM, address: '127.0.0.1:62125', transport: 'socket'
MyBean類上配置了MyTestAnnotation注解!
test
Disconnected from the target VM, address: '127.0.0.1:62125', transport: 'socket'
參考鏈接:https://blog.csdn.net/zt15732625878/article/details/99671586
參考鏈接:https://blog.csdn.net/zt15732625878/article/details/100061528
趙小胖個人博客
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/176790.html
標籤:Java
上一篇:Spring Boot Sample 003之spring-boot-configuration-properties
下一篇:爬蟲之requests庫
