国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

通過代碼實例了解SpringBoot啟動原理

2024-07-09 22:42:57
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了通過代碼實例了解SpringBoot啟動原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

SpringBoot和Spring相比,有著不少優勢,比如自動配置,jar直接運行等等。那么SpringBoot到底是怎么啟動的呢?

下面是SpringBoot啟動的入口:

@SpringBootApplicationpublic class HelloApplication {   public static void main(String[] args) {    SpringApplication.run(HelloApplication.class, args);  }}

一、先看一下@SpringBoot注解:

@Target({ElementType.TYPE})  //定義其使用時機@Retention(RetentionPolicy.RUNTIME) //編譯程序將Annotation儲存于class檔中,可由VM使用反射機制的代碼所讀取和使用。@Documented //這個注解應該被 javadoc工具記錄@Inherited //被注解的類會自動繼承. 更具體地說,如果定義注解時使用了 @Inherited 標記,然后用定義的注解來標注另一個父類, 父類又有一個子類(subclass),則父類的所有屬性將被繼承到它的子類中.@SpringBootConfiguration //@SpringBootConfiguration就相當于@Configuration。JavaConfig配置形式@EnableAutoConfiguration@ComponentScan(  excludeFilters = {@Filter(  type = FilterType.CUSTOM,  classes = {TypeExcludeFilter.class}), @Filter(  type = FilterType.CUSTOM,  classes = {AutoConfigurationExcludeFilter.class})} //自動掃描并加載符合條件的組件。以通過basePackages等屬性來細粒度的定制@ComponentScan自動掃描的范圍,如果不指定,則默認Spring框架實現會從聲明@ComponentScan所在類的package進行掃描。注:所以SpringBoot的啟動類最好是放在root package下,因為默認不指定basePackages。)public @interface SpringBootApplication {  @AliasFor(    annotation = EnableAutoConfiguration.class  )  Class<?>[] exclude() default {};  @AliasFor(    annotation = EnableAutoConfiguration.class  )  String[] excludeName() default {};  @AliasFor(    annotation = ComponentScan.class,    attribute = "basePackages"  )  String[] scanBasePackages() default {};  @AliasFor(    annotation = ComponentScan.class,    attribute = "basePackageClasses"  )  Class<?>[] scanBasePackageClasses() default {};}

所以,實際上SpringBootApplication注解相當于三個注解的組合,@SpringBootConfiguration,@ComponentScan和@EnableAutoConfiguration。

@SpringBootConfiguration和@ComponentScan,很容易知道它的意思,一個是JavaConfig配置,一個是掃描包。關鍵在于@EnableAutoConfiguration注解。先來看一下這個注解:

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})public @interface EnableAutoConfiguration {  String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";  Class<?>[] exclude() default {};  String[] excludeName() default {};}

Springboot應用啟動過程中使用ConfigurationClassParser分析配置類時,如果發現注解中存在@Import(ImportSelector)的情況,就會創建一個相應的ImportSelector對象, 并調用其方法 public String[] selectImports(AnnotationMetadata annotationMetadata), 這里 EnableAutoConfigurationImportSelector的導入@Import(EnableAutoConfigurationImportSelector.class) 就屬于這種情況,所以ConfigurationClassParser會實例化一個 EnableAutoConfigurationImportSelector 并調用它的 selectImports() 方法。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孙吴县| 九龙城区| 普定县| 油尖旺区| 荔浦县| 左贡县| 都兰县| 江川县| 孙吴县| 密山市| 无棣县| 疏勒县| 清新县| 景东| 钟祥市| 隆昌县| 尉氏县| 仙桃市| 柳河县| 吴江市| 门头沟区| 始兴县| 沙湾县| 嘉禾县| 乌恰县| 车险| 松江区| 天长市| 石门县| 大埔县| 赣榆县| 军事| 庄河市| 张家口市| 寿光市| 工布江达县| 班玛县| 依兰县| 嘉鱼县| 云和县| 沙洋县|