springBoot常见面试题,springboot是如何进行自动装配的,如何内置tomcat并运行的?

 springBoot常见面试题,springboot是如何进行自动装配的,如何内置tomcat并运行的?

 应用启动,1、使用了SpringApplication.run方法,Application配置了@SpringBootApplication的注解,从这两个方面去分析。

 primarySource:指定了需要加载的资源;SpringApplication进行资源的加载处理

args:就是main方法的入参,return :一个正在运行的applicationContext。

SpringApplication根据primarySource中的各种配置进行加载,加载完后,然后启动项目

step1:看看primarySource是如何配置相关的默认配置的,首先就是有一@SpringBootApplication的注解

 需要注意的有@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

@SpringBootConfiguration:重点有一个@Configuration的注解,而@Configuration实际是一个@Component的注解,这个注解我们大家就比较熟悉了,就是spring定义bean的注解,所以@SpringBootConfiguration代表配置的ioc容器,比如我们配置的@Controller就是其中之一

 @EnableAutoConfiguration是用于开启自动装配的

@Import:导入一个component classes,导入的是AutoConfigurationImportSelector

AutoConfigurationImportSelector:一个处理自动装载的选择器,调用方法路径如下:

selectImports()->getAutoConfigurationEntry(annotationMetadata)->getCandidateConfigurations(annotationMetadata, attributes)

通过getCandidateConfigurations确定读取配置的位置为 META-INF/spring.factories

 

 spring.factories配置了需要自动装载的配置,一般是默认AutoConfiguration结尾。

常用的几个AutoConfiguration分析:

EmbeddedWebServerFactoryCustomizerAutoConfiguration,其中包含tomcat的相关配置

 WebMvcAutoConfiguration:webmvc的相关配置

ServletWebServerFactoryAutoConfiguration:自动装配为了servlet web服务/for servlet web servers

 TransactionAutoConfiguration:spring事务的自动装配。

@ComponentScan:默认扫描的范围是application的package,以及其子包

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值