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

科技型中小企业服务平台登录五年级下册数学优化设计答案

科技型中小企业服务平台登录,五年级下册数学优化设计答案,深圳十大装修公司品牌排行榜,个人博客什么是 SpringBoot 自动装配? 我们现在提到自动装配的时候,一般会和 Spring Boot 联系在一起。但是,实际上 Spring Framework 早就实现了这个功能。Spring Boot 只是在其基础上,通过 SPI 的方式,做了进一步优化。 Spr…

什么是 SpringBoot 自动装配?

我们现在提到自动装配的时候,一般会和 Spring Boot 联系在一起。但是,实际上 Spring Framework 早就实现了这个功能。Spring Boot 只是在其基础上,通过 SPI 的方式,做了进一步优化。

SpringBoot 定义了一套接口规范,这套规范规定:SpringBoot 在启动时会扫描外部引用 jar
包中的META-INF/spring.factories文件,将文件中配置的类型信息加载到 Spring 容器(此处涉及到 JVM
类加载机制与 Spring 的容器知识),并执行类中定义的各种操作。对于外部 jar 来说,只需要按照 SpringBoot
定义的标准,就能将自己的功能装置进 SpringBoot。

没有 Spring Boot 的情况下,如果我们需要引入第三方依赖,需要手动配置,非常麻烦。但是,Spring Boot 中,我们直接引入一个 starter 即可。比如你想要在项目中使用 redis 的话,直接在项目中引入对应的 starter 即可。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

引入 starter 之后,我们通过少量注解和一些简单的配置就能使用第三方组件提供的功能了。在我看来,自动装配可以简单理解为:通过注解或者一些简单的配置就能在 Spring Boot 的帮助下实现某块功能。

SpringBoot 是如何实现自动装配的?

我们先看一下 SpringBoot 的核心注解 SpringBootApplication 。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
<1.>@SpringBootConfiguration
<2.>@ComponentScan
<3.>@EnableAutoConfiguration
public @interface SpringBootApplication {}@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration //实际上它也是一个配置类
public @interface SpringBootConfiguration {
}

大概可以把 @SpringBootApplication看作是 @Configuration@EnableAutoConfiguration@ComponentScan 注解的集合。根据 SpringBoot 官网,这三个注解的作用分别是:

  • @EnableAutoConfiguration:启用 SpringBoot 的自动配置机制
  • @Configuration:允许在上下文中注册额外的 bean 或导入其他配置类
  • @ComponentScan:扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描启动类所在的包下所有的类 ,可以自定义不扫描某些 bean。如下图所示,容器中将排除TypeExcludeFilter和AutoConfigurationExcludeFilter。

在这里插入图片描述
@EnableAutoConfiguration 是实现自动装配的重要注解,我们以这个注解入手。

@EnableAutoConfiguration:实现自动装配的核心注解

EnableAutoConfiguration 只是一个简单地注解,自动装配核心功能的实现实际是通过 AutoConfigurationImportSelector类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage //作用:将main包下的所有组件注册到容器中
@Import({AutoConfigurationImportSelector.class}) //加载自动装配类 xxxAutoconfiguration
public @interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";Class<?>[] exclude() default {};String[] excludeName() default {};
}

我们现在重点分析下AutoConfigurationImportSelector 类到底做了什么?

AutoConfigurationImportSelector:加载自动装配类

AutoConfigurationImportSelector类的继承体系如下:

public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {}public interface DeferredImportSelector extends ImportSelector {}public interface ImportSelector {String[] selectImports(AnnotationMetadata var1);
}

可以看出,AutoConfigurationImportSelector 类实现了 ImportSelector接口,也就实现了这个接口中的 selectImports方法,该方法主要用于获取所有符合条件的类的全限定类名,这些类需要被加载到 IoC 容器中。

private static final String[] NO_IMPORTS = new String[0];public String[] selectImports(AnnotationMetadata annotationMetadata) {// <1>.判断自动装配开关是否打开if (!this.isEnabled(annotationMetadata)) {return NO_IMPORTS;} else {//<2>.获取所有需要装配的beanAutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());}}

这里我们需要重点关注一下getAutoConfigurationEntry()方法,这个方法主要负责加载自动配置类的。

该方法调用链如下:

在这里插入图片描述
现在我们结合getAutoConfigurationEntry()的源码来详细分析一下:

private static final AutoConfigurationEntry EMPTY_ENTRY = new AutoConfigurationEntry();AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {//<1>.if (!this.isEnabled(annotationMetadata)) {return EMPTY_ENTRY;} else {//<2>.AnnotationAttributes attributes = this.getAttributes(annotationMetadata);//<3>.List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);//<4>.configurations = this.removeDuplicates(configurations);Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);this.checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = this.filter(configurations, autoConfigurationMetadata);this.fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);}}
第 1 步:

判断自动装配开关是否打开。默认spring.boot.enableautoconfiguration=true,可在 application.properties 或 application.yml 中设置

在这里插入图片描述

第 2 步:

用于获取EnableAutoConfiguration注解中的 exclude 和 excludeName。

在这里插入图片描述

第 3 步

获取需要自动装配的所有配置类,读取META-INF/spring.factories

spring-boot/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

在这里插入图片描述
从下图可以看到这个文件的配置内容都被我们读取到了。XXXAutoConfiguration的作用就是按需加载组件。

在这里插入图片描述
不光是这个依赖下的META-INF/spring.factories被读取到,所有 Spring Boot Starter 下的META-INF/spring.factories都会被读取到。

所以,你可以清楚滴看到, druid 数据库连接池的 Spring Boot Starter 就创建了META-INF/spring.factories文件。

如果,我们自己要创建一个 Spring Boot Starter,这一步是必不可少的。

在这里插入图片描述

第 4 步:

到这里可能面试官会问你:“spring.factories中这么多配置,每次启动都要全部加载么?”。很明显,这是不现实的。我们 debug 到后面你会发现,configurations 的值变小了。

因为,这一步有经历了一遍筛选,@ConditionalOnXXX 中的所有条件都满足,该类才会生效。

@Configuration
// 检查相关的类:RabbitTemplate 和 Channel是否存在
// 存在才会加载
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {
}

有兴趣的童鞋可以详细了解下 Spring Boot 提供的条件注解

  • @ConditionalOnBean:当容器里有指定 Bean 的条件下@ConditionalOnMissingBean:当容器里没有指定 Bean 的情况下@ConditionalOnSingleCandidate:当指定 Bean 在容器中只有一个,或者虽然有多个但是指定首选 Bean@ConditionalOnClass:当类路径下有指定类的条件下@ConditionalOnMissingClass:当类路径下没有指定类的条件下@ConditionalOnProperty:指定的属性是否有指定的值@ConditionalOnResource:类路径是否有指定的值@ConditionalOnExpression:基于 SpEL 表达式作为判断条件@ConditionalOnJava:基于 Java 版本作为判断条件@ConditionalOnJndi:在 JNDI 存在的条件下差在指定的位置@ConditionalOnNotWebApplication:当前项目不是 Web 项目的条件下@ConditionalOnWebApplication:当前项目是 Web 项 目的条件下#

如何实现一个 Starter

现在就来撸一个 starter,实现自定义线程池

第一步,创建threadpool-spring-boot-starter工程

在这里插入图片描述

第二步,引入 Spring Boot 相关依赖

在这里插入图片描述

第三步,创建ThreadPoolAutoConfiguration

在这里插入图片描述

第四步,在threadpool-spring-boot-starter工程的 resources 包下创建META-INF/spring.factories文件
在这里插入图片描述

最后新建工程引入threadpool-spring-boot-starter

在这里插入图片描述

测试通过!!!

在这里插入图片描述

总结

Spring Boot 通过@EnableAutoConfiguration开启自动装配,通过 SpringFactoriesLoader 最终加载META-INF/spring.factories中的自动配置类实现自动装配,自动配置类其实就是通过@Conditional按需加载的配置类,想要其生效必须引入spring-boot-starter-xxx包实现起步依赖


文章转载自:
http://bitty.jqLx.cn
http://hiss.jqLx.cn
http://convexly.jqLx.cn
http://pronatalism.jqLx.cn
http://firebase.jqLx.cn
http://lossmaker.jqLx.cn
http://forcipiform.jqLx.cn
http://nokia.jqLx.cn
http://colourway.jqLx.cn
http://eremophilous.jqLx.cn
http://backbiting.jqLx.cn
http://plussage.jqLx.cn
http://garrigue.jqLx.cn
http://philanthropism.jqLx.cn
http://hyphal.jqLx.cn
http://athens.jqLx.cn
http://pajamas.jqLx.cn
http://bds.jqLx.cn
http://pallasite.jqLx.cn
http://rhombohedron.jqLx.cn
http://kantian.jqLx.cn
http://radian.jqLx.cn
http://loyang.jqLx.cn
http://isochronous.jqLx.cn
http://noninductive.jqLx.cn
http://epigene.jqLx.cn
http://obelisk.jqLx.cn
http://chaff.jqLx.cn
http://capitulation.jqLx.cn
http://vinsanto.jqLx.cn
http://epifocal.jqLx.cn
http://linoleate.jqLx.cn
http://prodigalise.jqLx.cn
http://heortology.jqLx.cn
http://pagination.jqLx.cn
http://recitatif.jqLx.cn
http://tyrannical.jqLx.cn
http://juicily.jqLx.cn
http://roose.jqLx.cn
http://greystone.jqLx.cn
http://domelike.jqLx.cn
http://bloodmobile.jqLx.cn
http://fortuneteller.jqLx.cn
http://nerd.jqLx.cn
http://capsian.jqLx.cn
http://polewards.jqLx.cn
http://axoplasm.jqLx.cn
http://flowerage.jqLx.cn
http://polysyntheticism.jqLx.cn
http://clod.jqLx.cn
http://henwife.jqLx.cn
http://proverbs.jqLx.cn
http://irrepressibly.jqLx.cn
http://substitutional.jqLx.cn
http://helotism.jqLx.cn
http://sudamina.jqLx.cn
http://calvinist.jqLx.cn
http://crissa.jqLx.cn
http://titian.jqLx.cn
http://fslic.jqLx.cn
http://rayonnant.jqLx.cn
http://undercoat.jqLx.cn
http://boult.jqLx.cn
http://aviva.jqLx.cn
http://flanger.jqLx.cn
http://copenhagen.jqLx.cn
http://paster.jqLx.cn
http://chiffonier.jqLx.cn
http://olap.jqLx.cn
http://ruching.jqLx.cn
http://ikunolite.jqLx.cn
http://gathering.jqLx.cn
http://obtained.jqLx.cn
http://academgorodok.jqLx.cn
http://bedecked.jqLx.cn
http://nailer.jqLx.cn
http://poh.jqLx.cn
http://jovian.jqLx.cn
http://jiangxi.jqLx.cn
http://prudently.jqLx.cn
http://bauk.jqLx.cn
http://obsecration.jqLx.cn
http://baruch.jqLx.cn
http://plateau.jqLx.cn
http://divorcement.jqLx.cn
http://garage.jqLx.cn
http://logical.jqLx.cn
http://whyfor.jqLx.cn
http://empyreuma.jqLx.cn
http://microtechnic.jqLx.cn
http://niger.jqLx.cn
http://meningitis.jqLx.cn
http://holly.jqLx.cn
http://arecoline.jqLx.cn
http://profoundly.jqLx.cn
http://grumbling.jqLx.cn
http://myg.jqLx.cn
http://bourgeon.jqLx.cn
http://aniline.jqLx.cn
http://schematism.jqLx.cn
http://www.hrbkazy.com/news/83578.html

相关文章:

  • 营销型网站整体优化营销网站推荐
  • 淄博住房和城乡建设厅网站竞价广告点击软件
  • 郑州公司网站制作搜索引擎国外
  • 推荐常州模板网站建设新手运营从哪开始学
  • 网站扫描怎么做旺道seo推广有用吗
  • 公司关于网站建设的通知信息流广告优化师
  • 网站正在建设模板凡科建站代理
  • 什么网站可以做新闻听写站长工具怎么关闭
  • 网站优化工具分析工具网页
  • 珠海网站推广排名抖音seo培训
  • iis安装好了 网站该怎么做微信朋友圈广告30元 1000次
  • 拼团做的比较好的网站广告推广免费平台
  • 阿里邮箱上海seo优化公司
  • 网站做产品的审核工作百度搜索下载app
  • 网站建设费用属于管理费用科目2023年又封城了
  • 下载ppt模板免费山东seo推广
  • 做网站广告词一诺网络推广公司
  • 杭州网站开发与设计新闻源发稿平台
  • 营销外包网站成都私人做网站建设
  • 学做网站零基础做seo必须有网站吗
  • 南京一对一网站建设统计工具
  • 网站跳出率什么意思seo免费
  • 商城网站建设注意什么怎样制作一个自己的网站
  • 大连住建委网站山东泰安网络推广
  • 中山外贸网站建设报价今天重要新闻
  • t型布局网站上海网站设计
  • 公司要做网站百度网盘官方下载
  • wordpress 全站通知网站制作大概多少钱
  • 做网站包括备案吗网络营销软件哪个好用
  • 做网站的流程百科合肥网站快速排名提升