日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

@SpringBootApplication注解的使用

瀏覽:21日期:2023-03-18 09:53:17
一、前言

大部分的配置都可以用Java類+注解來代替,而在SpringBoot項目中見的最多的莫過于@SpringBootApplication注解了,它在每個SpringBoot的啟動類上都有標注。

這個注解對SpringBoot的啟動和自動配置到底有什么樣的影響呢?本文將為各位大佬解析它的源碼,揭開@SpringBootApplication注解神秘的面紗。

二、正文

對SpringBoot工程的自動配置很感興趣,于是學習其源碼并整理了其中一些內容,如果有錯誤請大家指正~話不多說,直接上源碼;

@SpringBootApplication注解的源碼如下:

@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 {...}

可以看到這是一個復合注解,一共包括7個不同的注解,下面對這7個不同的注解進行分析。

2.1 注解2.1.1 注解1:@Target({ElementType.TYPE})

用來表示注解作用范圍,TYPE表示作用范圍為類或接口。

@SpringBootApplication注解的使用

2.1.2 注解2:@Retention(RetentionPolicy.RUNTIME)

@SpringBootApplication注解的使用

2.1.3 注解3:@Documented

表明這個注釋是由 javadoc記錄的。

2.1.4 注解4:@Inherited

放在注解上,當父類加了@SpringBootApplication注解時,子類也會繼承這個注解(對接口的實現類無效)。

2.1.5 注解5:@SpringBootConfiguration

底層仍是@Configuration注解, 源碼如下:

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Configurationpublic @interface SpringBootConfiguration {}2.1.6 注解6:@ComponetScan

@ComponentScan這個注解在Spring中很重要,它對應XML配置中的元素@ComponentScan的功能其實就是自動掃描并加載符合條件的組件(比如@Component和@Repository等)或者bean定義,最終將這些bean定義加載到IoC容器中。

可以通過basePackages等屬性來細粒度的定制@ComponentScan自動掃描的范圍,如果不指定,則默認Spring框架實現會從聲明@ComponentScan所在類的package進行掃描。所以SpringBoot的啟動類最好是放在root package下,因為默認不指定basePackages。

2.2 注解:@EnableAutoConfiguration

個人感覺@EnableAutoConfiguration這個Annotation最為重要它的作用可以概括為:借助@Import的幫助,將所有符合自動配置條件的bean定義加載到IoC容器。

其源碼如下:

@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 {};}

這里需要關注@AutoConfigurationPackage和@Import(AutoConfigurationImportSelector.class)兩個注解。

2.2.1 注釋:@AutoConfigurationPackage

源碼如下:

@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 {};}

可以發現這個注解的核心其實也是Import注解,表示對于標注該注解的類的包,應當使用AutoConfigurationPackages注冊。接著看Registrar這個類:

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {​ @Override //metadata是我們注解所在的元信息 public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { //將我們注解所在包下所有的組件進行注冊 register(registry, new PackageImport(metadata).getPackageName()); }​ @Override public Set<Object> determineImports(AnnotationMetadata metadata) { return Collections.singleton(new PackageImport(metadata)); }}

這個類中的核心方法是register方法:

private static final String BEAN = AutoConfigurationPackages.class.getName(); public static void register(BeanDefinitionRegistry registry, String... packageNames) { if (registry.containsBeanDefinition(BEAN)) { BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN); ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues(); constructorArguments.addIndexedArgumentValue(0, addBasePackages(constructorArguments, packageNames));​ } else { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(BasePackages.class); beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, packageNames); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); registry.registerBeanDefinition(BEAN, beanDefinition); }}

register方法的邏輯非常清晰:如果這個bean已經被注冊,就獲取它的構造函數參數值,并將包名添加進去;否則就創建一個新的bean定義并進行注冊。通過@AutoConfigurationPackage這個注解,可以將注解所在包下所有的組件進行注冊。

2.2.2 注解:@Import(AutoConfigurationImportSelector.class)

這個注解導入了AutoConfigurationImportSelector這個類這個類的核心方法是selectImports方法,實現ImportSelector接口。方法基于我們在pom.xml文件中配置的jar包和組件進行導入。所以方法返回的是一個Class全路徑的String數組,返回的Class會被Spring容器管理。方法源碼如下:

@Overridepublic String[] selectImports(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader .loadMetadata(this.beanClassLoader); AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata); return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());}

這個方法的結構也很清晰,首先通過isEnabled方法判斷是否需要進行導入,如果需要導入的話,通過loadMetadata方法獲取配置信息,并通過getAutoConfigurationEntry進行自動裝配。isEnabled方法源碼如下:

protected boolean isEnabled(AnnotationMetadata metadata) { if (getClass() == AutoConfigurationImportSelector.class) { return getEnvironment().getProperty(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class, true); } return true;}

這個方法通過EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY這個配置項進行判斷是否需要自動配置,默認為true。loadMetadata方法源碼如下:

protected static final String PATH = 'META-INF/' + 'spring-autoconfigure-metadata.properties';​ public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) { return loadMetadata(classLoader, PATH); }​ static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader, String path) { try { Enumeration<URL> urls = (classLoader != null) ? classLoader.getResources(path) : ClassLoader.getSystemResources(path); Properties properties = new Properties(); while (urls.hasMoreElements()) {properties.putAll(PropertiesLoaderUtils.loadProperties(new UrlResource(urls.nextElement()))); } return loadMetadata(properties); } catch (IOException ex) { throw new IllegalArgumentException('Unable to load @ConditionalOnClass location [' + path + ']', ex); } } static AutoConfigurationMetadata loadMetadata(Properties properties) { return new PropertiesAutoConfigurationMetadata(properties); }

可以看到這個方法會加載META-INF/spring-autoconfigure-metadata.properties下的所有配置信息并包裝成AutoConfigurationMetadata對象返回。

注:spring-autoconfigure-metadata.properties文件在spring-boot-autoconfigure-2.1.9.RELEASE.jar/META-INF下。

getAutoConfigurationEntry方法源碼如下:

protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } AnnotationAttributes attributes = getAttributes(annotationMetadata); List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); configurations = removeDuplicates(configurations); Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return new AutoConfigurationEntry(configurations, exclusions);}

這個方法是AutoConfiguration的主流程方法,可以將這個方法的每一行看做一個步驟,那么處理步驟如下:

1. 加載配置了@EnableAutoConfiguration注解的屬性值getAttribute方法:

protected AnnotationAttributes getAttributes(AnnotationMetadata metadata) { String name = getAnnotationClass().getName(); AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(name, true)); Assert.notNull(attributes, () -> 'No auto-configuration attributes found. Is ' + metadata.getClassName()+ ' annotated with ' + ClassUtils.getShortName(name) + '?'); return attributes;}

2.得到META-INF/spring.factories文件中以@EnableAutoConfiguration完全限定類名做key的value,getCandidateConfigurations方法:

​protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),getBeanClassLoader()); Assert.notEmpty(configurations, 'No auto configuration classes found in META-INF/spring.factories. If you '+ 'are using a custom packaging, make sure that file is correct.'); return configurations;}protected Class<?> getSpringFactoriesLoaderFactoryClass() { return EnableAutoConfiguration.class;}

其中,SpringFactoriesLoader.loadFactoryNames()這個方法的作用是使用給定的類加載器從META-INF/spring.factories加載給定類型的工廠實現的完全限定類名;

3.去重;

4.得到需要排除的類的類名,這些類可以在@EnableAutoConfiguration注解中配置;

5.檢查這兩個集合;

6.把需要排除的類移除;

7.根據OnBeanCondition、OnClassCondition等條件進行過濾(有興趣可以深入了解);

8.廣播事件,得到AutoConfigurationImportListener所有實現類,然后生成事件進行廣播;

9.把需要裝配和排除的類完全限定名封裝成了AutoConfigurationEntry對象返回。

因此,@EnableAutoConfiguration可以簡單總結為:從classpath中搜尋所有的META-INF/spring.factories配置文件,并將其中EnableAutoConfiguration對應的配置項通過反射實例化為對應的標注了@Configuration的IoC容器配置類,并加載到IoC容器。

三、小結

通過以上分析可知@SpringBootApplication注解的運作是通過@SpringApplicationConfiguration聲明被標注類為配置類,從而被AnnotationConfigApplicationContext掃描并初始化Spring容器。

通過@EnableAutoConfiguration來掃描,過濾并加載所需要的組件;通過@ComponentScan掃描并注冊所有標注了@Component及其子注解的類;這些注解的共同運作實現了springboot工程強大的自動配置能力。

以上就是@SpringBootApplication注解的使用的詳細內容,更多關于@SpringBootApplication注解的使用的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲涩涩av| 国产精区一区二区| 精品国产91| 麻豆久久一区| 麻豆精品久久久| 精品欠久久久中文字幕加勒比| 国产日韩一区二区三区在线播放| 日本少妇一区二区| 青青草国产精品亚洲专区无| 日韩精品一区二区三区中文字幕| 美女久久网站| 亚洲精品在线a| 日产欧产美韩系列久久99| 日韩精品第二页| 国产精品入口久久| 日韩综合在线| 欧美一区在线观看视频| 国产精品成人一区二区网站软件| 卡一卡二国产精品| 国产精品99精品一区二区三区∴| 五月天久久网站| 91久久国产| 亚洲欧美日韩综合国产aⅴ| 亚洲毛片在线免费| 国产精品国产三级在线观看| 免费一级欧美在线观看视频 | 欧美激情在线精品一区二区三区| 国产精品男女| 成人在线观看免费视频| 日韩精品永久网址| 樱桃成人精品视频在线播放| 综合色一区二区| 视频一区中文字幕| 久久的色偷偷| 影视先锋久久| 日韩精品久久理论片| 老司机精品视频网| 欧洲av一区二区| 亚欧洲精品视频在线观看| 久久av免费| 久久人人99| 日韩三区四区| 新版的欧美在线视频| 综合一区av| 97精品国产| 丝袜美腿高跟呻吟高潮一区| 免费在线观看一区| 在线亚洲精品| 国产精品激情电影| 在线综合视频| 精品久久国产一区| 美女精品网站| 精品一区二区三区免费看| 亚洲电影在线一区二区三区| 日韩av黄色在线| 久久高清免费| 国产精品欧美三级在线观看 | 精品视频网站| 亚洲欧美视频| 日韩1区2区| 少妇精品在线| 日本精品不卡| 久久精品xxxxx| 夜夜精品视频| 国产精品成人a在线观看| 日韩影院免费视频| 国产在线一区不卡| 亚洲精品乱码| 亚洲第一精品影视| 久久av超碰| 日韩精品一级二级| 日韩国产综合| 国产精品久久久久久模特| 欧美精品一卡| 高清一区二区| 欧美一区久久| 蜜乳av另类精品一区二区| 美女视频免费精品| 亚洲精品看片| 在线综合视频| 秋霞影院一区二区三区| 欧美精品影院| 美女久久网站| 欧美亚洲激情| 成人在线视频中文字幕| 日韩高清一区二区| 国产精品日韩| 少妇精品导航| 精品香蕉视频| 国产日产一区| 亚洲精品乱码久久久久久蜜桃麻豆 | 亚洲精品**中文毛片| 亚洲精选av| 午夜久久黄色| 日韩免费视频| 久久精品九色| 国产精品视频一区二区三区四蜜臂| 一区二区精品| 中文日韩在线| 极品日韩av| 国产在线|日韩| 激情黄产视频在线免费观看| 国产高清亚洲| 久久国产欧美日韩精品| 日韩中文字幕1| 国产午夜久久| 91成人精品| 欧美中文一区二区| 欧美日韩水蜜桃| 久久久久国产精品一区三寸| 裤袜国产欧美精品一区| 精品国产a一区二区三区v免费| 国产精品一区二区中文字幕| 婷婷成人av| 日韩高清成人在线| 亚洲三级av| 日本在线观看不卡视频| 日韩一区二区三区四区五区| 亚洲另类视频| 亚洲欧美久久精品| 日韩精品三级| 久久精品99国产精品日本| 日韩动漫一区| 欧美偷窥清纯综合图区| 欧美片第1页综合| 国产精品地址| 精品日韩一区| 四季av一区二区凹凸精品| 精品三区视频| 超碰在线99| 在线一区视频观看| 99国产精品免费视频观看| 精品捆绑调教一区二区三区| 日韩精品首页| 色老板在线视频一区二区| 久久精选视频| 好看的av在线不卡观看| 五月精品视频| 亚洲午夜免费| 久久狠狠久久| 精品久久在线| 久久婷婷亚洲| 天堂成人免费av电影一区| 美国欧美日韩国产在线播放| 亚洲字幕久久| 国产精品一区亚洲| 国产一区二区色噜噜| 日韩在线观看一区| 在线日韩电影| 国产精品视区| 日韩av午夜在线观看| 精品国产精品久久一区免费式 | 综合色就爱涩涩涩综合婷婷| 欧美三区不卡| 六月婷婷综合| 在线亚洲国产精品网站| 日韩欧美中文在线观看| 久久99蜜桃| 免费福利视频一区二区三区| 免费欧美一区| 日韩毛片一区| 国产精品国产一区| 欧美日韩精品一本二本三本| 亚洲久久视频| 国产精品1区在线| 久久国产电影| 日韩动漫一区| 蜜桃av.网站在线观看| 国产一区导航| 国产亚洲人成a在线v网站| 色网在线免费观看| 丝袜美腿一区二区三区| 麻豆久久久久久| 欧美日韩国产高清| 国产精品多人| 亚洲精品一区二区妖精| 日韩av中文字幕一区二区三区| 成人台湾亚洲精品一区二区| 国产一区二区高清| 国产亚洲精品美女久久 | 亚洲色图网站| 四虎成人av| 亚洲精品一二| 久久国产日韩| 国产精品任我爽爆在线播放 | 黄色av一区| 日本午夜免费一区二区| 99久久精品网站| 国产精品美女久久久久久不卡| 99久久激情| 国产精品magnet| 久久一二三区| 日韩成人亚洲| 国产欧美日韩综合一区在线播放| 日韩av一级| 久久99久久久精品欧美| 石原莉奈一区二区三区在线观看| 成人精品国产亚洲| 91亚洲无吗| 夜夜精品视频|