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

SpringBoot面试宝典

SpringBoot

1、核心启动流程

入口点:@SpringBootApplication

@SpringBootApplication 是 Spring Boot 项目的核心入口注解,是三个注解的组合

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration  // 1. 标记为配置类
@EnableAutoConfiguration  // 2. 启用自动配置
@ComponentScan(           // 3. 组件扫描excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})}
)
public @interface SpringBootApplication {// ... 属性配置
}

@SpringBootConfiguration

  • 作用:标识当前类为 Spring Boot 的配置类
  • 等价于@Configuration(Spring 标准配置注解)
  • 特点
    • 类中可以使用 @Bean 定义 Bean
    • 配置类本身也会被注册为 Bean

@EnableAutoConfiguration

  • 核心机制:启用 Spring Boot 的自动配置魔法
  • 工作原理
    1. 加载 META-INF/spring.factories 文件中的配置
    2. 过滤符合条件的自动配置类(通过 @Conditional 系列注解)
    3. 根据类路径和已有 Bean 动态配置应用
条件满足
条件不满足
应用启动
加载spring.factories
筛选AutoConfiguration类
条件检查
应用自动配置
跳过配置
创建相关Bean

@ComponentScan

  • 作用:自动扫描并注册组件

  • 默认行为:扫描当前包及其子包中的组件

    • @Component
    • @Service
    • @Repository
    • @Controller
  • 自定义扫描路径

    @SpringBootApplication(scanBasePackages = "com.example.myapp")
    // 或
    @SpringBootApplication(scanBasePackageClasses = {MyService.class, MyController.class})
    
SpringApplication.run () 核心步骤
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {return run(new Class<?>[] { primarySource }, args);
}public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {// 创建 SpringApplication 实例并执行 run 方法return new SpringApplication(primarySources).run(args);
}public ConfigurableApplicationContext run(String... args) {// 1. 创建计时器和监听器StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();// 2. 配置无头模式(Headless Mode)configureHeadlessProperty();// 3. 获取并启动应用监听器SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting();try {// 4. 解析命令行参数ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 5. 准备环境(配置属性源、激活配置文件)ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);// 6. 打印 BannerBanner printedBanner = printBanner(environment);// 7. 创建应用上下文(Servlet 或 Reactive)context = createApplicationContext();// 8. 加载异常报告器exceptionReporters = getSpringBootExceptionReporters(context);// 9. 准备上下文(注册单例 Bean、应用后置处理器)prepareContext(context, environment, listeners, applicationArguments, printedBanner
http://www.dtcms.com/a/328141.html

相关文章:

  • MySQL User表入门教程
  • Spyglass CDC rule
  • NLP—词向量转换评论学习项目分析
  • 28.分类算法:让机器学会分类
  • Tauri Qt孰优孰劣
  • ncurses 6.5 交叉编译移植到OpenHarmomy
  • 2025年渗透测试面试题总结-16(题目+回答)
  • Linux文件系统:从虚拟接口到物理实现的架构解析
  • 【C#】用队列构建一个对象池管理对象的创建和释放
  • NumPy 快速入门与实战教程(逐行拆解 + 专业扩展)
  • 详细了解sklearn中的CountVectorizer
  • 【c++深入系列】:万字详解模版(下)
  • [Robotics_py] 路径规划算法 | 启发式函数 | A*算法
  • MaxKB+合合信息TextIn:通过API实现PDF扫描件的文档审核
  • LRU算法与LFU算法
  • 农业智慧大屏系统 - Flask + Vue实现
  • 云计算分类与主流产品
  • iOS 文件管理全流程实战,从开发调试到数据迁移
  • 初识神经网络04——构建神经网络2
  • 软路由雷达:基于OpenWrt系统的传统逆向思路的冷门实现
  • 外部中断寄存器的实现-库函数版(STC8)
  • Android性能优化:架构层面的性能考量
  • 【第13话:泊车感知】场景检测与分割:自主泊车场景中的检测及语义分割方法
  • Linux中的tar 和crontab命令
  • 需求的透明化如何实现
  • Threejs 设置灯光照射点位置 辅助器不跟随移动
  • 基于MATLAB实现的PSO优化BP神经网络
  • Java数据结构之数组
  • 电商双 11 美妆数据分析学习报告
  • 锅气:「现炒之魂·烟火人间」