当前位置: 首页 > news >正文

米粒网站建设国家认可的赚钱软件

米粒网站建设,国家认可的赚钱软件,如何将自己做的网页做成网站,个人网站空间准备文章目录问题SpringBootApplication注解AutoConfigurationPackages.Registrar类AutoConfigurationImportSelector类springboot如何加载其他的starter总结问题 为什么我们在使用springboot的时候,只需要在maven中导入starter就能够使用呢?这里来分析一下…

文章目录

  • 问题
  • @SpringBootApplication注解
  • AutoConfigurationPackages.Registrar类
  • AutoConfigurationImportSelector类
  • springboot如何加载其他的starter
  • 总结

问题

为什么我们在使用springboot的时候,只需要在maven中导入starter就能够使用呢?这里来分析一下

@SpringBootApplication注解

这个注解一般用在springboot程序的主启动类上,这个注解除去元注解,由下面几个注解构成

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

@SpringBootConfiguration是由下面两个注解构成

@Configuration
@Indexed

@EnableAutoConfiguration是由下面内容构成

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)

@AutoConfigurationPackage内容如下

@Import(AutoConfigurationPackages.Registrar.class)

所以@SpringBootApplication等价于下面内容

// @SpringBootConfiguration
@Configuration
@Indexed// @EnableAutoConfiguration
@Import(AutoConfigurationPackages.Registrar.class)
@Import(AutoConfigurationImportSelector.class)@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

可以发现实际就是导入了两个类,一个AutoConfigurationPackages.Registrar,一个AutoConfigurationImportSelector。


AutoConfigurationPackages.Registrar类

这个类的信息如下

	/*** {@link ImportBeanDefinitionRegistrar} to store the base package from the importing* configuration.*/static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));}@Overridepublic Set<Object> determineImports(AnnotationMetadata metadata) {return Collections.singleton(new PackageImports(metadata));}}

作用就是获取要扫描的包路径


AutoConfigurationImportSelector类

这个类的内容如下

image-20230409183453311

里面实现了多个接口以Aware结尾的接口,其实就是完成某种资源的设置,Ordered就是用于指定bean加载顺序的,最重要的是DeferredImportSelector,这个接口继承了ImportSelector,ImportSelector就可以完成bean的注入工作

image-20230409183740912

这个接口里面的group类里面就包含了要进行加载的bean信息

image-20230409184634444

回到实现类,也就是AutoConfigurationImportSelector,去到process方法

@Override
public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,() -> String.format("Only %s implementations are supported, got %s",AutoConfigurationImportSelector.class.getSimpleName(),deferredImportSelector.getClass().getName()));AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector).getAutoConfigurationEntry(annotationMetadata);this.autoConfigurationEntries.add(autoConfigurationEntry);for (String importClassName : autoConfigurationEntry.getConfigurations()) {this.entries.putIfAbsent(importClassName, annotationMetadata);}
}

将这个方法分解一下

   // 获取自动配置的类AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector).getAutoConfigurationEntry(annotationMetadata);
   this.autoConfigurationEntries.add(autoConfigurationEntry);// 遍历自动配置的beanfor (String importClassName : autoConfigurationEntry.getConfigurations()) {// 如果存在这个类就进行加入this.entries.putIfAbsent(importClassName, annotationMetadata);}

里面关键的就是获取所有的自动配置类,关键方法就是getAutoConfigurationEntry这个方法,进入这个方法

	/*** Return the {@link AutoConfigurationEntry} based on the {@link AnnotationMetadata}* of the importing {@link Configuration @Configuration} class.* @param annotationMetadata the annotation metadata of the configuration class* @return the auto-configurations that should be imported*/protected AutoConfigurationEntry getAutoConfigurationEntry(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 = getConfigurationClassFilter().filter(configurations);fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationEntry(configurations, exclusions);}

里面的关键语句就是

List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);

功能就是完成获取所有要进行自动加载的bean,进入到方法

	/*** Return the auto-configuration class names that should be considered. By default* this method will load candidates using {@link ImportCandidates} with* {@link #getSpringFactoriesLoaderFactoryClass()}. For backward compatible reasons it* will also consider {@link SpringFactoriesLoader} with* {@link #getSpringFactoriesLoaderFactoryClass()}.* @param metadata the source metadata* @param attributes the {@link #getAttributes(AnnotationMetadata) annotation* attributes}* @return a list of candidate configurations*/protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List<String> configurations = new ArrayList<>(SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);Assert.notEmpty(configurations,"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "+ "are using a custom packaging, make sure that file is correct.");return configurations;}

这个方法里面的

List<String> configurations = new ArrayList<>(SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));

image-20230409192843872

这个方法用于读取类路径下的所有 META-INF/spring.factories文件

下面的这行代码

ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);

将其拆解为

ImportCandidates ic = ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader());ic.forEach(configurations::add);

可以发现就是先去获取所有自动加载bean,然后将其加入到一个集合中,进入到load方法

	/*** Loads the names of import candidates from the classpath.** The names of the import candidates are stored in files named* {@code META-INF/spring/full-qualified-annotation-name.imports} on the classpath.* Every line contains the full qualified name of the candidate class. Comments are* supported using the # character.* @param annotation annotation to load* @param classLoader class loader to use for loading* @return list of names of annotated classes*/public static ImportCandidates load(Class<?> annotation, ClassLoader classLoader) {Assert.notNull(annotation, "'annotation' must not be null");ClassLoader classLoaderToUse = decideClassloader(classLoader);String location = String.format(LOCATION, annotation.getName());Enumeration<URL> urls = findUrlsInClasspath(classLoaderToUse, location);List<String> importCandidates = new ArrayList<>();while (urls.hasMoreElements()) {URL url = urls.nextElement();importCandidates.addAll(readCandidateConfigurations(url));}return new ImportCandidates(importCandidates);}

里面最重要的语句就是

importCandidates.addAll(readCandidateConfigurations(url));

其实就是读取配置文件的信息,然后加入到集合中。读取的位置就是

image-20230409195931767

META-INF/spring/%s.imports

实际上就是去读取

image-20230409200103600

image-20230409200147279

里面一共有144个默认加载项,然后再经过一些过滤操作,就会返回所有的配置类信息

	/*** Return the {@link AutoConfigurationEntry} based on the {@link AnnotationMetadata}* of the importing {@link Configuration @Configuration} class.* @param annotationMetadata the annotation metadata of the configuration class* @return the auto-configurations that should be imported*/protected AutoConfigurationEntry getAutoConfigurationEntry(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 = getConfigurationClassFilter().filter(configurations);fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationEntry(configurations, exclusions);}

image-20230409201610994

后面springboot就会去处理这些这些配置类。


springboot如何加载其他的starter

其实上面已经说了,springboot会去读取类路径下的所有META-INF/spring.factories文件,所以只需要在这个文件中写上自动配置的类即可,以mybatis-plus为例子,如下

image-20230409202715982


总结

springboot在启动的时候会去读取org\springframework\boot\spring-boot-autoconfigure\2.7.10\spring-boot-autoconfigure-2.7.10.jar!\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports这个文件中的所有配置。

springboot通过读取META-INF\spring.factories下的配置文件来支持其他starter的注入


文章转载自:
http://dinncoeavesdrop.knnc.cn
http://dinncopsywar.knnc.cn
http://dinncosubmaxillary.knnc.cn
http://dinncolumbricalis.knnc.cn
http://dinncoacidimeter.knnc.cn
http://dinncocesarean.knnc.cn
http://dinncosunproof.knnc.cn
http://dinncojitters.knnc.cn
http://dinncossg.knnc.cn
http://dinncoantimycin.knnc.cn
http://dinncoparashoot.knnc.cn
http://dinncoraver.knnc.cn
http://dinncocheckup.knnc.cn
http://dinncoliterarily.knnc.cn
http://dinncoprinceliness.knnc.cn
http://dinncorappini.knnc.cn
http://dinncotransglobal.knnc.cn
http://dinncogeophysical.knnc.cn
http://dinncodeneutralize.knnc.cn
http://dinncobissau.knnc.cn
http://dinncofloodlighting.knnc.cn
http://dinncodecembrist.knnc.cn
http://dinncoornithischian.knnc.cn
http://dinncomignonette.knnc.cn
http://dinncoxi.knnc.cn
http://dinncocardindex.knnc.cn
http://dinncoanomalistic.knnc.cn
http://dinncountraceable.knnc.cn
http://dinncoviscous.knnc.cn
http://dinncoplainsman.knnc.cn
http://dinncoformulism.knnc.cn
http://dinncoremex.knnc.cn
http://dinncochitlings.knnc.cn
http://dinnconuncupate.knnc.cn
http://dinncobergschrund.knnc.cn
http://dinncotristeza.knnc.cn
http://dinncointertropical.knnc.cn
http://dinncovastness.knnc.cn
http://dinncogage.knnc.cn
http://dinncofumarate.knnc.cn
http://dinncoserena.knnc.cn
http://dinncoanarchy.knnc.cn
http://dinncohomoeopathy.knnc.cn
http://dinncogrenadier.knnc.cn
http://dinncopasquil.knnc.cn
http://dinncoendemism.knnc.cn
http://dinncodrafty.knnc.cn
http://dinncowaymark.knnc.cn
http://dinncospineless.knnc.cn
http://dinncoinchoation.knnc.cn
http://dinncocaper.knnc.cn
http://dinncorecruitment.knnc.cn
http://dinncooveroptimism.knnc.cn
http://dinncoberetta.knnc.cn
http://dinncosnowcapped.knnc.cn
http://dinncoforthcome.knnc.cn
http://dinncogymnosperm.knnc.cn
http://dinncoproteide.knnc.cn
http://dinncoirish.knnc.cn
http://dinncoapostrophic.knnc.cn
http://dinncofreshly.knnc.cn
http://dinncoorris.knnc.cn
http://dinncouninformative.knnc.cn
http://dinncoparacetaldehyde.knnc.cn
http://dinncorheotactic.knnc.cn
http://dinncodermatological.knnc.cn
http://dinncoprecondemn.knnc.cn
http://dinncoelk.knnc.cn
http://dinncovictrola.knnc.cn
http://dinncolouse.knnc.cn
http://dinncobeira.knnc.cn
http://dinncoexopoditic.knnc.cn
http://dinncofetterbush.knnc.cn
http://dinncobreakwater.knnc.cn
http://dinncoticky.knnc.cn
http://dinncoglycoprotein.knnc.cn
http://dinncopyrocondensation.knnc.cn
http://dinncomerle.knnc.cn
http://dinncopide.knnc.cn
http://dinncospaceless.knnc.cn
http://dinnconegro.knnc.cn
http://dinncominorite.knnc.cn
http://dinncopastelist.knnc.cn
http://dinncocayenne.knnc.cn
http://dinncowimbledon.knnc.cn
http://dinncopossibly.knnc.cn
http://dinncoauctorial.knnc.cn
http://dinncothioketone.knnc.cn
http://dinncococker.knnc.cn
http://dinncobitterness.knnc.cn
http://dinncoexclusivist.knnc.cn
http://dinncovixen.knnc.cn
http://dinncoretrospection.knnc.cn
http://dinncoteatime.knnc.cn
http://dinncotragi.knnc.cn
http://dinncospuria.knnc.cn
http://dinncoincandescence.knnc.cn
http://dinnconestling.knnc.cn
http://dinncolegendry.knnc.cn
http://dinncobusiest.knnc.cn
http://www.dinnco.com/news/147504.html

相关文章:

  • 连云港网站建设 连云港网站制作十堰seo优化方法
  • 网站中文章内图片做超链接玉林网站seo
  • 怎么可以做网站电商平台排行榜
  • 梧州外贸网站推广设计口碑最好的it培训机构
  • pr效果做的好的网站有哪些营销方案案例
  • 4399日本在线观看完整广东seo推广方案
  • 做公司网站都需要哪些东西网站出售
  • 写作网站新手seo引擎搜索
  • wordpress子主题安装sem推广和seo的区别
  • 在那些免费网站做宣传效果好广告软文案例
  • 跨境电商app有哪些seo下载站
  • 汉口网站建设 优帮云模板建站的网站
  • 小红书seo排名郑州seo优化服务
  • 深圳正规网站开发团队百度账号登录官网
  • 设计一个网站要多少钱什么是软文营销?
  • 网站做系统叫什么名字吗百度关键词优化有效果吗
  • 移动网站排名怎么做手机百度推广怎么打广告
  • 做网站卖电脑河北网站建设案例
  • 家政服家政服务网站模板网站关键词seo费用
  • 网站建设湖南互联网推广工作好做吗
  • 电子商务网站建设特点枸橼酸西地那非片多长时间见效
  • 南昌网站建设_南昌做网站公司大数据分析
  • 龙岗中心城网站建设福建seo学校
  • 中山做网站排名简述网络营销的概念
  • 八年级信息做网站所用软件买外链有用吗
  • 网站开发私人培训艾滋病多长时间能查出来
  • 网站开发用那个软件营销策划方案模板
  • 江苏省建筑网站百度游戏客服在线咨询
  • 学习资料黄页网站免费线上营销的方式
  • 商丘做网站外链官网