博客
关于我
SpringBoot总结(二)——Spring Boot的自动配置
阅读量:810 次
发布时间:2019-03-25

本文共 699 字,大约阅读时间需要 2 分钟。

从启动类入手,观察其上方标注的 @SpringBootApplication 注解意义重大。这是一个组合注解,其内部组成包括 @SpringBootConfiguration 和 @EnableAutoConfiguration,确保应用具备Spring Boot的标准配置。

细解 @SpringBootApplication 内部机制,首先关注 @SpringBootConfiguration 注解。这一模块性注解为Spring Boot应用设定了主配置类定位规则,即标注该注解的类即为应用的主要Spring配置类。继而,@EnableAutoConfiguration 打开了自动配置功能,这相当于让Spring Boot自自动配置各模块而不需要手动声明。

自动配置的实现精髓在于 @AutoConfigurationPackage。该注解通过 @Import 导入 AutoConfigurationPackages.Registrar 类,其职责是扫描指定包及其子包,收集所有符合条件的组件。系统运行时会读取 META-INF/spring.factories 檔案,解析里面的配置信息,将扫描结果整合到Spring容器。

值得注意的是,通过设置 debug 模式,可以深入探究特定组件的实现原理。特别是如何通过 AutoConfigurationImportSelector 类选择需要导入的配置。

在整体架构中,@SpringBootApplication 标注的主配置类自动成为包扫描范围内的一部分。其所在包及子包中的所有组件都会被引入到Spring容器中,确保配置文件功能齐全。

转载地址:http://wkayk.baihongyu.com/

你可能感兴趣的文章
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>
npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
查看>>
npm—小记
查看>>
npm介绍以及常用命令
查看>>
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>