1. 概述
自从推出以来,Spring Boot 就一直是 Spring 生态系统中不可或缺的一部分。它凭借自动配置(auto-configuration)等特性,极大地简化了开发流程。
本篇文章将围绕 Spring Boot 在技术面试中常见的问题进行总结和解析,帮助你快速回顾核心知识点。
2. 核心问题解析
Q1. 什么是 Spring Boot?它有哪些核心特性?
Spring Boot 是基于 Spring Framework 构建的快速开发框架。它通过自动配置、内嵌服务器支持、丰富的文档和社区资源,成为了 Java 领域最受欢迎的技术之一。
主要特性包括:
- Starters(启动器) – 一组依赖描述符,用于快速引入所需依赖。
- Auto-configuration(自动配置) – 根据类路径下的依赖自动配置应用。
- Actuator(监控) – 提供生产级监控功能,如健康检查、指标等。
- Security(安全) – 支持认证和授权。
- Logging(日志) – 提供统一的日志记录机制。
Q2. Spring 与 Spring Boot 有什么区别?
Spring 是一个功能强大的框架,提供了依赖注入、AOP、数据绑定、数据访问等功能。
但随着功能的增加,Spring 的配置也变得越来越复杂。Spring Boot 则通过“约定优于配置”的理念,简化了 Spring 的使用。
✅ Spring Boot 的优势:
- 根据类路径中的依赖自动配置应用
- 提供生产级特性,如安全、健康检查等
Q3. 如何使用 Maven 搭建 Spring Boot 项目?
最推荐的方式是继承 spring-boot-starter-parent
,并在 pom.xml
中声明所需的 Starters:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
</parent>
⚠️ 注意:如果公司有统一的父 POM,也可以使用自定义父 POM 的方式来管理依赖。
Q4. 什么是 Spring Initializr?
Spring Initializr 是一个快速生成 Spring Boot 项目的工具。
你可以访问 https://start.spring.io 来选择语言、构建工具、依赖等,然后下载项目骨架。
✅ 无论是手动创建还是通过 IDE(如 STS)创建,背后其实都是调用 Initializr。
Q5. Spring Boot 提供了哪些 Starters?
所有 Starters 都以 spring-boot-starter-*
命名,位于 org.springframework.boot
组下,便于查找和管理。
常见 Starters 包括:
spring-boot-starter
– 核心启动器,包含自动配置、日志、YAMLspring-boot-starter-web
– 用于构建 Web 应用(含 REST)spring-boot-starter-data-jpa
– 使用 JPA 和 Hibernatespring-boot-starter-security
– Spring Security 支持spring-boot-starter-test
– 测试支持
Q6. 如何禁用某个自动配置?
可以通过 @EnableAutoConfiguration
或 @SpringBootApplication
的 exclude
属性来禁用特定的自动配置类:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { }
或者在 application.properties
中设置:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Q7. 如何注册自定义自动配置?
需要在 META-INF/spring.factories
文件中注册:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration
Q8. 如何让自动配置在 Bean 已存在时自动退出?
使用 @ConditionalOnMissingBean
注解即可:
@Bean
@ConditionalOnMissingBean
public CustomService service() { ... }
Q9. 如何打包 Spring Boot 应用为 JAR 或 WAR?
默认打包为 JAR,使用 spring-boot-maven-plugin
:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
打包为 WAR 需要修改 pom.xml
:
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Q10. 如何开发命令行应用?
只需在主类中调用 SpringApplication.run()
:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
Q11. 外部配置的来源有哪些?
Spring Boot 支持多种外部配置来源:
application.properties
或application.yml
- 环境变量
- JVM 系统属性
- 命令行参数
可通过 @Value
、@ConfigurationProperties
或 Environment
获取。
Q12. 什么是 Relaxed Binding(宽松绑定)?
Spring Boot 支持属性名的多种写法,例如:
myProp
my-prop
my_prop
MY_PROP
它们都可以绑定到同一个 Java 属性上。
Q13. Spring Boot DevTools 是做什么的?
DevTools 是一组开发时工具,支持热部署、LiveReload 等功能:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
✅ 开发时自动重启、浏览器自动刷新,极大提升开发效率。
Q14. 如何编写集成测试?
使用 @SpringBootTest
注解即可加载上下文:
@SpringBootTest
class MyApplicationTests {
// test methods
}
Q15. Spring Boot Actuator 是干什么的?
Actuator 提供生产级监控功能,如健康检查、指标、环境信息等:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
常见端点包括:
/actuator/health
/actuator/metrics
/actuator/env
Q16. Properties 和 YAML 哪个更好?
YAML 优势:
- 层级清晰,可读性强
- 支持嵌套结构
- 一个文件可定义多个 profile(Spring Boot 2.4+)
⚠️ 缺点是缩进要求严格,容易出错。
Q17. Spring Boot 提供了哪些核心注解?
常用注解包括:
@EnableAutoConfiguration
– 启用自动配置@SpringBootApplication
– 主类标记,整合了@Configuration
+@EnableAutoConfiguration
+@ComponentScan
Q18. 如何更改默认端口?
可以通过以下方式修改:
- 配置文件:
server.port=8081
- 启动类设置:
SpringApplication.setAdditionalProfiles()
- 命令行参数:
java -jar -Dserver.port=8081 app.jar
Q19. Spring Boot 支持哪些内嵌服务器?如何切换?
默认支持:
- Spring MVC:Tomcat(默认)、Jetty、Undertow
- WebFlux:Reactor Netty(默认)、Tomcat、Jetty、Undertow
切换示例(从 Tomcat 切到 Jetty):
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Q20. 为什么要使用 Spring Profiles?
不同环境(Dev、QA、Prod)的配置往往不同,使用 Profile 可以轻松隔离:
application-dev.properties
application-prod.properties
通过 spring.profiles.active=prod
指定当前环境。
3. 总结
这篇文章涵盖了 Spring Boot 面试中常见的核心问题,从基本概念到实际应用均有涉及。掌握这些内容,面试时将更有底气。
✅ 熟练掌握这些知识点,拿下心仪 Offer 不再是梦!