Annotation(註解),用於爲Java代碼提供元數據。簡單理解註解能夠看作是一個個標籤,用來標記代碼。是一種應用於類、方法、參數、變量、構造器及包的一種特殊修飾符。html
元註解就是註解到註解上的註解,或者說元註解是一種基本註解,它能用來註解其餘註解。java
咱們能夠將元註解當作一種特殊的修飾符,用來解釋說明註解,它是註解的元數據。git
@Documentedgithub
被@Documented修飾的Annotation類將會被javadoc工具提取成文檔。web
@Inheritedspring
被@Inherited修改的Annotation將具備繼承性,若是某個類使用了@MyAnnotation註解(定義該Annotation時使用了@Inherited修飾)修飾,則其子類將自動被@MyAnnotation修飾。微信
@Retentionapp
被@Retention修改的註解,結合RetentionPolicy.XXX能夠指定該註解存在的聲明週期。ide
SOURCE:僅存在Java源碼文件,通過編譯器後便丟棄函數
CLASS:存在Java源文件,以及通過編譯器後生成的Class字節碼文件,但在運行時JVM中再也不保留
RUNTIME:存在源文件、變異生成的Class字節碼文件,以及保留在運行時JVM中,能夠經過反射讀取註解信息
@Target
表示該註解類型所使用的程序元素類型,結合ElementType.XXX來使用。
@Repeatable
Java8新增的可重複註解。
@Override
用於告知編譯器,咱們須要覆寫超類的當前方法。
@Deprecated
使用這個註解,用於告知編譯器,某一程序元素(好比方法,成員變量)不建議使用了(即過期了)。
@SuppressWarnings
用於告知編譯器忽略特定的警告信息,例在泛型中使用原生數據類型,編譯器會發出警告,當使用該註解後,則不會發出警告。
@FunctionalInterface
用戶告知編譯器,檢查這個接口,保證該接口是函數式接口,即只能包含一個抽象方法,不然就會編譯出錯。
格式
成員變量
定義:用無形參的方法形式來聲明,註解方法不能帶參數,好比name(),age()
類型:前面能夠用數據類型來修飾
默認值:成員變量能夠有默認值,好比default "spring boot"
註解使用
反射獲取類上的註解
官網見:18. Using the @SpringBootApplication Annotation
The @SpringBootApplication
annotation is equivalent to using @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
with their default attributes
@SpringBootConfiguration等同於@Configuration
@SpringBootConfiguration等同於@Configuration,@Configuration等同於@Component
@SpringBootConfiguration繼承自@Configuration,兩者功能也一致,標註當前類是配置類。
並會將當前類內聲明的一個或多個以@Bean註解標記的方法的實例歸入到Spring容器中,實例名就是方法名。
@Configuration
: allow to register extra beans in the context or import additional configuration classes
能夠直接經過context.getBean("getMap")的方式獲取。
@Configuration等同與@Component
官網見[Spring Framework Core]:1.10.1. @Component
and Further Stereotype Annotations
conclusion
:@Component includes @Configuration,@Repository,@Service and @Controller
@ComponentScan
: enable @Component
scan on the package where the application is located (see the best practices)
官網見[Spring Framework Core]:1.10.3. Automatically Detecting Classes and Registering Bean Definitions
@ComponentScan主要就是定義掃描的路徑以及子路徑中,找出標識了須要裝配的類自動裝配到Spring的bean容器中。
官網見:17. Spring Beans and Dependency Injection
If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan
without any arguments. All of your application components (@Component
, @Service
, @Repository
, @Controller
etc.) are automatically registered as Spring Beans.
@EnableAutoConfiguration
: enable Spring Boot’s auto-configuration mechanism
官網見: 11.3.2 The @EnableAutoConfiguration Annotation
The second class-level annotation is @EnableAutoConfiguration
. This annotation tells Spring Boot to 「guess」 how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web
added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
藉助AutoConfigurationImportSelector,@EnableAutoConfiguration能夠幫助Spring Boot應用將全部符合條件的@Configuration配置都加載到IoC容器中
selectImports方法
讀取候選裝配組件getCandidateConfigurations
SpringFacotriesLoader.loadFactoryNames
點開loadSpringFactories
點開FACTORIES_RESOURCE_LOCATION
官網見:49.1 Understanding Auto-configured Beans
Under the hood, auto-configuration is implemented with standard
@Configuration
classes. Additional@Conditional
annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use@ConditionalOnClass
and@ConditionalOnMissingBean
annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own@Configuration
.
You can browse the source code of
spring-boot-autoconfigure
to see the@Configuration
classes that Spring provides (see theMETA-INF/spring.factories
file).
官網見:49.3 Condition Annotations
You almost always want to include one or more
@Conditional
annotations on your auto-configuration class. The@ConditionalOnMissingBean
annotation is one common example that is used to allow developers to override auto-configuration if they are not happy with your defaults.Spring Boot includes a number of
@Conditional
annotations that you can reuse in your own code by annotating@Configuration
classes or individual@Bean
methods. These annotations include:
翻譯
:不一樣類型的Conditional
(1)ConditionalOnClass:當且僅當ClassPath存在指定的Class時,才建立標記上該註解的類的實例
(2)ConditionalOnBean: 當且僅當指定的bean classes and/or bean names在當前容器中,才建立標記上該註解的類的實例
(3)ConditionalOnProperty:當且僅當Application.properties存在指定的配置項時,建立標記上了該註解的類的實例
(4)ConditionalOnResource:在classpath下存在指定的resource時建立
(5)ConditionalOnWebApplication:在web環境下建立
你們掃描下方二維碼關注下個人微信公衆號,公衆號內沒有福利,只會按期生產技術性文章!