@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
@SpringBootApplication: Spring Boot應用標註在某個類上說明這個類是SpringBoot的主配置類,java
SpringBoot 就應該運行這個類的main方法來啓動SpringBoot應用;spring
相關配置啓動都是由該註解來幫咱們完成的,點進去了解一下數據庫
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { }
點進去會發現他的註解類當中還有不少註解,就是一個自定義組合註解。springboot
接下來來對他組合的註解一一講解。
《2020最新Java基礎精講視頻教程和學習路線!》mvc
@Target說明了Annotation(註解)所修飾的對象範圍
取值(ElementType)有:框架
1.CONSTRUCTOR:用於描述構造器
2.FIELD:用於描述域
3.LOCAL_VARIABLE:用於描述局部變量
4.METHOD:用於描述方法
5.PACKAGE:用於描述包
6.PARAMETER:用於描述參數
7.TYPE:用於描述類、接口(包括註解類型) 或enum聲明
註解按生命週期來劃分可分爲3類:jvm
一、RetentionPolicy.SOURCE:註解只保留在源文件,當Java文件編譯成class文件的時候,註解被遺棄;
二、RetentionPolicy.CLASS:註解被保留到class文件,但jvm加載class文件時候被遺棄,這是默認的生命週期;
三、RetentionPolicy.RUNTIME:註解不只被保存到class文件中,jvm加載class文件以後,仍然存在;
這個註解只是用來標註生成javadoc的時候是否會被記錄。學習
在自定義註解的時候能夠使用@Documented來進行標註,若是使用@Documented標註了,在生成javadoc的時候就會把@Documented註解給顯示出來。spa
@Inherited是一個標識,用來修飾註解,自定義註解當中會用到.net
首先自定義一個註解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface ATable { public String name() default ""; }
如下是在使用自定義註解的一個場景。
類繼承關係中@Inherited的做用
類繼承關係中,子類會繼承父類使用的註解中被@Inherited修飾的註解
@ATable public class InheritedBase { } public class MyInheritedClass extends InheritedBase { }
接口繼承關係中@Inherited的做用
接口繼承關係中,子接口不會繼承父接口中的任何註解,無論父接口中使用的註解有沒有被@Inherited修飾
@ATable public interface IInheritedInterface { } public interface IInheritedInterfaceChild extends IInheritedInterface { }
類實現接口關係中@Inherited的做用
類實現接口時不會繼承任何接口中定義的註解
@ATable public interface IInheritedInterface { } public class MyInheritedClassUseInterface implements IInheritedInterface { }
標註在某個類上,表示這是一個Spring Boot的配置類
點進去會發現,他其實也是一個自定義註解
@Configuration學spring的應該對他不陌生
做用:指定當前類是一個配置類,在使用spring的時候剛開始都是xml配置,也正是這個註解,開啓了類配置方式。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
之前咱們須要配置的東西,Spring Boot會幫咱們自動配置;
@EnableAutoConfiguration告訴SpringBoot開啓自 動配置功能;這樣自動配置才能生效;
點進去會發現@Import,說白了他就是藉助@Import的支持,收集和註冊特定場景相關的bean定義。
@Import做用:用於導入其餘的配置類
而@EnableAutoConfiguration也是藉助@Import的幫助,將全部符合自動配置條件的bean定義加載到IoC容器,僅此而已!
@SuppressWarnings("deprecation") @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { }
EnableAutoConfigurationImportSelector:導入哪些組件的選擇器;
會給容器中導入很是多的自動配置類(xxxAutoConfiguration);
大概的流程就是:
Spring Boot在啓動的時候,經過EnableAutoConfigurationImportSelector類,從類路徑下的
META-INF/spring.factories中獲取EnableAutoConfiguration指定的值(就是上方截圖),
以全類名反射的建立方式,將這些值做爲自動配置類導入到容器中,自動配置類就生效,
幫咱們進行自動配置工做;
之前咱們須要本身配置的東西,自動配置類都幫咱們配置好了,這也就是使用springboot在使用spring,springmvc不用配置視圖解析器、數據庫鏈接池、事務 等配置的緣由。直接開箱即用。
固然springboot也給我提供了修改配置的方法,那就是經過yml或者propertie文件來進行修改springboot爲咱們配置好的配置默認值。
做用:用於經過註解指定spring在建立容器時要掃描的包
咱們能夠經過basePackages等屬性來細粒度的定製@ComponentScan自動掃描的範圍,若是不指定,則默認Spring框架實現會從聲明@ComponentScan所在類的package進行掃描。
@ComponentScan("com.gzl")
這也就是springboot啓動類爲何放在包外的緣由。
把@SpringBootApplication換成如下三個註解,照樣能夠正常啓動。
package com.gzl.cn; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
target=https%3A//blog.csdn.net/weixin_43888891)
連接地址: https://blog.csdn.net/weixin_43888891/article/details/110457235