Java 註解淺析

前言

    前段時間一直想了解下java的自定義註解,包括面試的時候也遇到過類似問題,今天總算有時間來學習下。

正文

註解(Annotation)概念

    註解是Java SE 5.0版本開始引入的概念,它是對java源代碼的說明,是一種元數據(描述數據的數據)。

註解和註釋的不同
  • 註釋
         註釋是對代碼的說明,給代碼的讀者看,便於幫讀者梳理業務邏輯;
  • 註解
        註解也是對代碼的說明,需要配合工具(解析它的代碼)使用,參與代碼的編譯,給應用程序看的;

爲什麼出現註解?

     在頻繁使用註解之前,是用xml 來作爲元數據使用,在最開始使用ssm(Spring,SpringMVC,Mybatis)框架時,bean與bean之間的依賴關係是通過xml文件來配置,這個xml文件和源代碼分散,當微服務、分佈式註解流行,系統越來越大,需要越來越多的xml文件來配置管理,文件配置冗長,而且類型不安全(運行期纔會發現錯誤),此時就需要一種比較簡單直觀而且類型安全的配置方式,註解就應運而生了,按照Springboot“約定大於配置”的方式,通過註解來約定其含義,更是減少了xml配置文件的數量。
     對於一些經常變動或者配置複雜的配置,使用xml文件來說是比較合適的,所以現在經常是註解和xml方式共存。

註解分類

    註解以@開頭,我們會在應用程序中見到各種各樣的註解,比如@Autowired,@Service,@Controller,@Override ,@Test,@Value等等,按照來源劃分,可以分爲 JDK的註解,第三方的註解,自定義註解。

  • JDK註解

    • Java 內置三大註解
      @Override (標記重寫方法)
      @Deprecated (標記過時)
      @SuppressWarnings (忽略警告)
    • 元註解 (註解的註解)
      @Target (註解的作用目標)
      @Retention (註解的生命週期)
      @Document (註解是否被包含在JavaDoc中)
      @Inherited (是否允許子類集成該註解)
  • 第三方註解(主要來源於各種框架)

    • Spring註解 @RequestMapping,@RestController,@Configuration,@Value,@Controller,@Service,@Repository,@Component等
    • SpringBoot註解
      @SpringBootApplication,@EnableAutoConfiguration等
    • JPA註解
      @Table,@Entity,@Column,@Id等
      ……
  • 自定義註解
    使用元註解自己定義的註解(下一篇文章將寫到實現自定義註解)

什麼是註解?

     在程序代碼中經常看到的以@ 開頭的大部分是註解;

註解定義的格式
修飾符 @interface 註解名 {
    註解元素的聲明1 
    註解元素的聲明2
}

     註解的元素聲明的兩種形式

 type elementName();
 type elementName() default value;  

註解定義實例

    我們來看下@Service 註解

// ElementType.TYPE 代表在註解上使用
@Target({ElementType.TYPE})
// RetentionPolicy.RUNTIME 代表運行時使用,可以通過反射獲取到
@Retention(RetentionPolicy.RUNTIME)
//包含在JavaDoc中
@Documented
//允許通過包掃描的方式自動檢測
@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 註解

package java.lang.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&trade; 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 {
}
 # The common interface extended by all annotation types.Also note that this interface does not itself define an annotation type.
 # 所有的註解默認繼承了Annotation接口,但是它本身不能定義註解。

    也就是說,我們見到的所有註解默認是一個實現了Annotation接口的接口。

參考文章

https://segmentfault.com/a/1190000018209300
https://blog.csdn.net/qq_36762677/article/details/82469741

總結

    在這個隨處可見註解的時代,還是要了解下註解的,感謝您的閱讀~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章