
简介本资源是面向Java后端工程师、Spring Boot初学者及架构师面试备考者的高频考点精编题集聚焦Spring Boot核心机制与工程实践难点。内容涵盖33道典型面试题及详细解析包括Spring Boot本质与优势、JavaConfig配置原理、Starter依赖管理、自动配置实现逻辑、DevTools热部署、Actuator监控端点、YAML配置特性、ActiveMQ集成、常用Starter分类及Spring Initializr项目创建等关键模块直击面试中对原理深度与落地能力的双重考察。资源为单文件Word文档.docx体积轻量仅25KB便于快速查阅与离线学习。目前已有319人下载学习适合作为面试前系统梳理知识脉络、查漏补缺与强化记忆的高效复习材料。1. 这不是题库是 Spring Boot 架构能力的「压力测试清单」33 道真题背后藏着的不是答案而是你是否真能把 Spring Boot 从脚手架用成生产级底盘你背过 SpringBootApplication 的三重组合注解但有没有在灰度发布时关掉某个 AutoConfiguration 导致 Redis 连接池没初始化你写过 application.yml 的多 profile 切换但有没有遇到过 bootstrap.yml 里配置了 Nacos 地址却因为 classpath 加载顺序被 application-dev.yml 覆盖而连不上配置中心这 33 道题表面是面试官在问“Spring Boot 是什么”实际是在用最小成本验证你写的每个 starter、每行配置、每次 actuator 调用是不是都踩在 Spring Boot 自动配置的逻辑链上而不是靠玄学跑通。它不考你能不能写出RestController而是考你改一行配置后整个 Bean 生命周期怎么塌方——比如把 spring.main.web-application-typereactive 写进 prod 环境结果嵌入式 Tomcat 启动成功但所有 HTTP 接口 404日志里连个 WARN 都没有。适合两类人一类是刚撕完三轮技术面、卡在架构设计环节被反问“你这个自动配置类为什么生效”的候选人另一类是带团队做微服务落地、发现新人总在 DevTools 热加载和 Actuator 端点之间反复横跳却说不清原理的 Tech Lead。这不是复习资料是帮你把 Spring Boot 从“能跑”拉到“敢压”的实操校验单。2. 从 starter 到自动配置拆解 Spring Boot 的「隐式契约」——为什么加一个依赖就自动配好 DataSource删一行配置就全崩2.1 Starter 不是依赖集合是「能力声明 配置契约」的二进制封装Spring Boot 的 starter 本质是 Maven 坐标 META-INF/spring.factories的组合体。以spring-boot-starter-data-jpa为例它不直接提供 Hibernate 实现而是通过pom.xml声明传递依赖如hibernate-core,spring-orm再在spring.factories中注册自动配置类# spring-boot-starter-data-jpa.jar/META-INF/spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration提示spring.factories中的EnableAutoConfiguration键值对才是 Spring Boot 扫描自动配置类的唯一入口。你手动添加的Import(MyConfig.class)不会触发条件化加载而 starter 里的类会。关键参数说明DataSourceAutoConfiguration仅当 classpath 存在javax.sql.DataSource且未定义DataSource类型 Bean 时才生效HibernateJpaAutoConfiguration需同时满足DataSource存在、LocalContainerEntityManagerFactoryBean未定义、且HibernateEntityManager或SessionFactory在 classpath若你项目中已手动 new 了一个HikariDataSource则DataSourceAutoConfiguration直接跳过后续 JPA 配置全部失效——这就是为什么删掉自定义 DataSource Bean 后spring.datasource.url突然生效的原因。2.2 自动配置的「条件引擎」ConditionalOnClass、ConditionalOnMissingBean 如何协同决策Spring Boot 的自动配置不是“全有或全无”而是由Conditional*注解组成的决策树。以WebMvcAutoConfiguration为例其生效需同时满足Configuration(proxyBeanMethods false) ConditionalOnWebApplication(type Type.SERVLET) ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) ConditionalOnMissingBean(WebMvcConfigurationSupport.class) public class WebMvcAutoConfiguration { ... }逻辑说明ConditionalOnWebApplication(type Type.SERVLET)排除 WebFlux 场景确保是 Servlet 容器ConditionalOnClass(...)必须存在DispatcherServlet否则不是 MVC 框架ConditionalOnMissingBean(WebMvcConfigurationSupport.class)若你继承了WebMvcConfigurationSupport并重写了addInterceptors()则此自动配置类被跳过所有默认拦截器如ResourceUrlProvider失效常见误用为加 Swagger 配置新建SwaggerConfig extends WebMvcConfigurationSupport结果导致静态资源/webjars/**404——因为WebMvcAutoConfiguration的ResourceHandlerRegistrationBean没加载而你没手动注册ResourceHandlerRegistry。2.3 配置属性绑定为什么spring.redis.host生效而redis.host失效Spring Boot 的ConfigurationProperties绑定依赖于prefix和Validated的严格匹配。以RedisProperties为例ConfigurationProperties(prefix spring.redis) public class RedisProperties { ... }这意味着application.yml中必须写spring.redis.host: 127.0.0.1写redis.host不会绑定若你自定义ConfigurationProperties(prefix my.redis)则必须对应my.redis.hostValidated注解启用 JSR-303 校验若spring.redis.port: abc启动时抛BindException而非静默忽略参数说明prefix必须与ConfigurationProperties声明完全一致大小写敏感若ConfigurationProperties类未被EnableConfigurationProperties显式启用或未被SpringBootApplication扫描到则绑定失败ConstructorBindingSpring Boot 2.2要求使用构造函数注入避免 setter 被反射调用导致 NPE。2.4 配置文件加载顺序为什么application-prod.yml里的spring.profiles.activedev让整个环境切回开发模式Spring Boot 的配置加载遵循「外部优先、profile 优先、后加载覆盖前加载」原则完整顺序为加载顺序位置示例是否可覆盖1java -jar app.jar --spring.config.locationfile:/config/外部指定路径✅2file:/config/application-{profile}.yml/config/application-prod.yml✅3file:/config/application.yml/config/application.yml✅4classpath:/config/application-{profile}.ymlBOOT-INF/classes/config/application-prod.yml✅5classpath:/config/application.ymlBOOT-INF/classes/config/application.yml✅6classpath:/application-{profile}.ymlBOOT-INF/classes/application-prod.yml✅7classpath:/application.ymlBOOT-INF/classes/application.yml✅关键陷阱spring.profiles.active本身是一个配置项它在第 1 步加载时就被解析用于决定后续哪些{profile}文件参与加载。因此若file:/config/application-prod.yml中写了spring.profiles.active: dev则第 2 步会去加载file:/config/application-dev.yml而非application-prod.ymlbootstrap.yml仅在 Spring Cloud 场景下生效且加载时机早于application.*用于配置中心地址等「元配置」application.properties和application.yml不能共存后者优先级更高前者会被忽略。3. DevTools 与 Actuator别让热加载和监控端点变成线上事故的导火索3.1 DevTools 的「双刃剑」机制为什么本地热加载快如闪电上线后却引发内存泄漏DevTools 的核心是RestartClassLoader它在检测到 classpath 变更时销毁旧 ClassLoader 并创建新实例但以下对象不会被回收静态变量引用的对象如static MapString, Object缓存ThreadLocal变量尤其 Tomcat 线程池中的RequestContextHolderJNI 资源如数据库连接池的 native socket 句柄典型翻车场景开发时用PostConstruct初始化一个ConcurrentHashMapkey 为用户 IDvalue 为 Session 对象DevTools 重启后旧 ClassLoader 持有的 Map 未被 GC新 ClassLoader 又建一个 Map内存持续增长线上若误启用 DevTools如spring.devtools.restart.enabledtrue3 天后 OOM解决方案强制禁用生产环境 DevTools在application-prod.yml中显式设置spring.devtools.restart.enabled: false使用RestartEndpoint替代文件监听通过 Actuator 的/actuator/restart端点触发可控重启避免在PostConstruct中持有长生命周期对象改用EventListener(ContextRefreshedEvent.class)动态注册。3.2 Actuator 端点的「权限裸奔」风险为什么/actuator/env泄露了数据库密码Actuator 默认暴露health和info但若开启management.endpoints.web.exposure.include*则所有端点包括/env,/beans,/configprops均对外可见。其中/actuator/env返回所有PropertySource包含systemEnvironment含PATH,JAVA_HOME和applicationConfig: [classpath:/application.yml]含明文密码/actuator/beans暴露所有 Bean 的类名、作用域、依赖关系攻击者可定位DataSourceBean 并构造反序列化链/actuator/loggers允许动态修改日志级别设为DEBUG可能刷爆磁盘安全加固步骤最小化暴露端点management: endpoints: web: exposure: include: health,info,metrics,prometheus增加认证Spring SecurityConfiguration public class ActuatorSecurity extends WebSecurityConfigurerAdapter { Override protected void configure(HttpSecurity http) throws Exception { http.requestMatcher(EndpointRequest.toAnyEndpoint()) .authorizeRequests((requests) - requests .requestMatchers(EndpointRequest.to(health, info)).permitAll() .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole(ACTUATOR)); } }禁用高危端点management: endpoint: env: show-values: NEVER # 默认为 WHEN_AUTHORIZED设为 NEVER 彻底隐藏3.3 自定义 Endpoint如何安全暴露业务指标而不暴露内部实现不要直接返回MapString, Object而应封装为不可变 DTOComponent Endpoint(id order-stats) public class OrderStatsEndpoint { private final OrderService orderService; public OrderStatsEndpoint(OrderService orderService) { this.orderService orderService; } ReadOperation public OrderStats getStats() { return new OrderStats( orderService.getTodayOrderCount(), orderService.getPendingPaymentCount(), Instant.now() // 避免返回系统时间戳防止时钟偏移推断 ); } // 不可变数据类无 setter字段 final public static class OrderStats { private final long todayOrders; private final long pendingPayments; private final Instant timestamp; public OrderStats(long todayOrders, long pendingPayments, Instant timestamp) { this.todayOrders todayOrders; this.pendingPayments pendingPayments; this.timestamp timestamp; } // getter only } }注意ReadOperation方法必须返回 POJOSpring Boot 会自动 JSON 序列化若返回String需手动ResponseBody且无法享受 Jackson 配置如JsonInclude。4. 避坑Spring Boot 面试高频翻车现场——33 题里藏了 8 个真实生产事故的影子4.1 现象SpringBootApplication(exclude DataSourceAutoConfiguration.class)后Transactional失效原因Transactional依赖TransactionAutoConfiguration而该类的ConditionalOnBean(PlatformTransactionManager.class)条件检查失败——因为DataSourceAutoConfiguration被排除导致DataSourceTransactionManager未创建进而PlatformTransactionManagerBean 缺失。解决明确排除TransactionAutoConfiguration或手动配置PlatformTransactionManagerBean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }4.2 现象application.yml中spring.profiles.active: prod生效但Profile(prod)的 Bean 未加载原因Profile注解作用于Component、Configuration等类但spring.profiles.active是运行时配置若Profile类被Import或ComponentScan扫描到却因Profile条件不满足被跳过则其内部Bean方法不会执行。排查启动时加-Ddebugtrue查看ConditionEvaluationReport中Profile的评估结果解决确保Profile(prod)类在ComponentScan路径下且spring.profiles.active在application.yml中正确设置非bootstrap.yml。4.3 现象spring-boot-starter-webflux和spring-boot-starter-web同时引入应用启动报错Port already in use原因两个 starter 分别尝试启动 NettyWebFlux和 TomcatWebMVC端口冲突。Spring Boot 不会自动选择其一而是并行启动。解决方案一彻底分离WebFlux 项目移除spring-boot-starter-web方案二强制指定 Web 类型在application.yml中spring: main: web-application-type: reactive # 或 servlet4.4 现象Scheduled方法在多实例部署时重复执行原因Scheduled是 JVM 级定时任务每个实例独立执行。若业务要求全局唯一如每天凌晨同步订单则需分布式锁。解决使用Scheduled Redis 分布式锁推荐RedissonLock改用消息队列如 RabbitMQ Delayed Message触发使用 Quartz 集群模式需数据库支持绝对禁止在Scheduled方法内加synchronized无效。4.5 现象spring-boot-devtools启用后Thymeleaf 模板修改不生效原因DevTools 的RestartClassLoader重新加载了TemplateResolver但 Thymeleaf 的TemplateCache仍持有旧模板的InputStream导致缓存未刷新。解决在application-dev.yml中强制关闭 Thymeleaf 缓存spring: thymeleaf: cache: false check-template: true或升级到 Spring Boot 2.4其 DevTools 已优化模板热加载逻辑。5. 从面试题到生产验证用 3 个命令验证你的 Spring Boot 理解是否穿透表层5.1 验证自动配置类是否生效/actuator/conditions端点的深度解读启动应用后访问http://localhost:8080/actuator/conditions?groupweb需暴露conditions端点返回 JSON 包含positiveMatches生效的自动配置类及匹配条件如WebMvcAutoConfiguration matchednegativeMatches未生效的类及原因如ReactiveWebServerFactoryConfiguration$EmbeddedTomcat did not match因ConditionalOnWebApplication(type REACTIVE)不满足关键字段message解释为何匹配/不匹配例如ConditionalOnClass found required class javax.servlet.Servlet血泪经验当某个功能如 Actuator不工作第一反应不是查文档而是 curl 这个端点——90% 的问题源于条件不满足而非代码错误。5.2 验证配置属性绑定/actuator/configprops与ConfigurationProperties的映射关系访问/actuator/configprops搜索spring.redis返回结构为{ contexts: { application: { beans: { spring.redis-org.springframework.boot.autoconfigure.data.redis.RedisProperties: { prefix: spring.redis, properties: { host: 127.0.0.1, port: 6379, password: null } } } } } }若password为null说明配置未加载若字段缺失如无database字段说明RedisProperties类中该字段未加Data或Getter/Setter验证技巧修改application.yml中spring.redis.password: test123刷新端点确认password字段变为test123删除spring.redis.host行刷新端点确认host字段变为localhost因RedisProperties中host有默认值。5.3 验证 Bean 生命周期/actuator/beans中的 scope、dependencies 与 lifecycle访问/actuator/beans搜索dataSource返回关键信息scope:singleton确认非 prototyperesource:class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]确认来自自动配置dependencies:[spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]确认依赖DataSourcePropertiesisPrimary:true确认为主数据源进阶技巧若dependencies中缺少DataSourceProperties说明spring.datasource.url未正确绑定若resource为com.example.MyDataSourceConfig说明你自定义的配置类生效自动配置被跳过搜索entityManagerFactory确认其dependencies包含dataSource验证 JPA 与 DataSource 的依赖链是否完整。6. 把面试题变成你的「故障响应手册」我如何用这 33 题构建团队的 Spring Boot 应急 SOP6.1 将每道题转化为「故障现象 → 检查清单 → 修复命令」三段式 SOP以第 5 题“如何重新加载更改而无需重启”为例我将其拆解为故障现象检查清单修复命令/操作修改 Controller 后接口返回旧数据1.curl http://localhost:8080/actuator/health确认应用存活2.ls -l target/classes/com/example/MyController.class查看文件修改时间3.jps -l | grep myapp确认进程 PIDkill -USR2 PID触发 DevTools 重启或mvn spring-boot:run -Dspring-boot.run.jvmArguments-XdebugDevTools 重启后内存占用翻倍1.jmap -histo:live PID | head -20查看对象数量2.jstat -gc PID观察 GC 频率3.curl http://localhost:8080/actuator/env | grep devtools在application-prod.yml中添加spring.devtools.restart.enabled: falsemanagement.endpoint.restart.show-details: always从那以后我每次上线新服务都强制走一遍这个 SOP 表格先 curl/actuator/health再jstat -gc看 GC最后curl /actuator/beans \| grep dataSource确认数据源加载——哪怕只是个 Hello World 服务。因为 80% 的线上问题根源都在启动那一刻的自动配置决策里而不是代码逻辑。希望帮到你。本文还有配套的精品资源点击获取