SpringBoot面试宝典

发布于:2025-08-16 ⋅ 阅读:(22) ⋅ 点赞:(0)

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. 打印 Banner
        Banner printedBanner = printBanner(environment);
        
        // 7. 创建应用上下文(Servlet 或 Reactive)
        context = createApplicationContext();
        
        // 8. 加载异常报告器
        exceptionReporters = getSpringBootExceptionReporters(context);
        
        // 9. 准备上下文(注册单例 Bean、应用后置处理器)
        prepareContext(context, environment, listeners, applicationArguments, printedBanner

网站公告

今日签到

点亮在社区的每一天
去签到