【秋招必备】Spring Boot面试题(2021最新版)
前言
在微服务理念席卷程序界的今天,作为最主流语言 Java 的一名 Coder,如何能正确的、系统的、快速的掌握 Spring Boot是至关重要的。接下来,让我们一起走进 Spring Boot的世界,一起感受 Spring Boot 的魅力。我坚信,机会永远属于有准备的人,我们与其羡慕他人的成功,不如从此刻起,积累足够多的知识和面试经验,为将来进入更好的公司做好充分的准备!
想让面试官在短短的几十分钟内认可你的能力?想在最短的时间内收获 Java 技术栈最核心的知识点?想要更全面更深入的了解 Java 技术?这篇文章给你想要的所有答案。
小编分享的这份Java后端开发面试总结包含了JavaOOP、Java集合容器、Java异常、并发编程、Java反射、Java序列化、JVM、Redis、Spring MVC、MyBatis、MySQL数据库、消息中间件MQ、Dubbo、Linux、ZooKeeper、 分布式&数据结构与算法等26个专题技术点,都是小编在各个大厂总结出来的面试真题,已经有很多粉丝靠这份PDF拿下众多大厂的offer,今天在这里总结分享给到大家!【已完结】
完整版Java面试题地址:2021最新面试题合集集锦。
1. 什么是SpringBoot?
通过Spring Boot,可以轻松地创建独立的,基于生产级别的Spring的应用程序,您可以“运行”它们。大 多数Spring Boot应用程序需要最少的Spring配置。
2. 为什么要用SpringBoot
快速开发,快速整合,配置简化、内嵌服务容器
3. SpringBoot与SpringCloud 区别
SpringBoot是快速开发的Spring框架,SpringCloud是完整的微服务框架,SpringCloud依赖于 SpringBoot。
4. Spring Boot 有哪些优点?
Spring Boot 主要有如下优点:
- 容易上手,提升开发效率,为 Spring 开发提供一个更快、更简单的开发框架。
- 开箱即用,远离繁琐的配置。
- 提供了一系列大型项目通用的非业务性功能,例如:内嵌服务器、安全管理、运行数据监 控、运行状况检查和外部化配置等。
- SpringBoot总结就是使编码变简单、配置变简单、部署变简单、监控变简单等等
5. Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?
启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:
- @SpringBootConfifiguration:组合了 @Confifiguration 注解,实现配置文件的功能。
- @EnableAutoConfifiguration:打开自动配置的功能,也可以关闭某个自动配置的选项, 例 如: java 如关闭数据源自动配置功能: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。
- @ComponentScan:Spring组件扫描。
6. Spring Boot 支持哪些日志框架?推荐和默认的日志框架是哪个?
Spring Boot 支持 Java Util Logging, Log4j2, Lockback 作为日志框架,如果你使用 Starters 启动器,Spring Boot 将使用 Logback 作为默认日志框架,但是不管是那种日志框架他都支持将配置文件输出到控制台或者文件中。
7. SpringBoot Starter的工作原理
- 我个人理解SpringBoot就是由各种Starter组合起来的,我们自己也可以开发Starter
- 在sprinBoot启动时由@SpringBootApplication注解会自动去maven中读取每个starter中的spring.factories文件,该文件里配置了所有需要被创建spring容器中的bean,并且进行自动配置把bean注入SpringContext中 //(SpringContext是Spring的配置文件)
8. Spring Boot 2.X 有什么新特性?与 1.X 有什么区别?
- 配置变更
- JDK 版本升级
- 第三方类库升级
- 响应式 Spring 编程支持
- HTTP/2 支持
- 配置属性绑定
- 更多改进与加强
9. SpringBoot支持什么前端模板,
thymeleaf,freemarker,jsp,官方不推荐JSP会有限制
10. SpringBoot的缺点
- 我觉得是为难人,SpringBoot在目前我觉得没有什么缺点,非要找一个出来我觉得就是
- 由于不用自己做的配置,报错时很难定位。
11. 运行 Spring Boot 有哪几种方式?
-
- 打包用命令或者放到容器中运行
-
- 用 Maven/ Gradle 插件运行
-
- 直接执行 main 方法运行
12. Spring Boot 需要独立的容器运行吗?
- 可以不需要,内置了 Tomcat/ Jetty 等容器。
13. 开启 Spring Boot 特性有哪几种方式?
- 继承spring-boot-starter-parent项目
- 导入spring-boot-dependencies项目依赖
14. SpringBoot 实现热部署有哪几种方式?

15. SpringBoot事物的使用
- SpringBoot的事物很简单,首先使用注解EnableTransactionManagement开启事物之后,然后在Service方法上添加注解Transactional便可。
16. Async异步调用方法
- 在SpringBoot中使用异步调用是很简单的,只需要在方法上使用@Async注解即可实现方法的异步调用。 注意:需要在启动类加入@EnableAsync使异步调用@Async注解生效。
17. 如何在 Spring Boot 启动的时候运行一些特定的代码?
- 可以实现接口 ApplicationRunner 或者 CommandLineRunner,这两个接口实现方式一样,它们都只提供了一个 run 方法
18. Spring Boot 有哪几种读取配置的方式?
- Spring Boot 可以通过 @PropertySource,@Value,@Environment, @ConfifigurationPropertie注解来绑定变量
19. 什么是 JavaConfig?

20. SpringBoot的自动配置原理是什么

21. 你如何理解 Spring Boot 配置加载顺序?

22. 什么是 YAML?
- YAML 是一种人类可读的数据序列化语言。它通常用于配置文件。与属性文件相比,如果我们想要在配置文件中添加复杂的属性,YAML 文件就更加结构化,而且更少混淆。可以看出 YAML 具有分层配置数据。
23. YAML 配置的优势在哪里 ?

24. Spring Boot 是否可以使用 XML 配置 ?
- Spring Boot 推荐使用 Java 配置而非 XML 配置,但是 Spring Boot 中也可以使用 XML 配置,通过 @ImportResource 注解可以引入一个 XML 配置。
25. spring boot 核心配置文件是什么?bootstrap.properties 和 application.properties 有何区别 ?

26. 什么是 Spring Profiles?

27. SpringBoot多数据源拆分的思路
- 先在properties配置文件中配置两个数据源,创建分包mapper,使用@ConfifigurationProperties读取properties中的配置,使用@MapperScan注册到对应的mapper包中
28. SpringBoot多数据源事务如何管理
- 第一种方式是在service层的@TransactionManager中使用transactionManager指定DataSourceConfifig中配置的事务
- 第二种是使用jta-atomikos实现分布式事务管理
29. 保护 Spring Boot 应用有哪些方法?
- 在生产中使用HTTPS
- 使用Snyk检查你的依赖关系
- 升级到最新版本
- 启用CSRF保护
- 使用内容安全策略防止XSS攻击
30. 如何实现 Spring Boot 应用程序的安全性?

31. 比较一下 Spring Security 和 Shiro 各自的优缺点 ?

32. Spring Boot 中如何解决跨域问题 ?
- 跨域可以在前端通过 JSONP 来解决,但是 JSONP 只可以发送 GET 请求,无法发送其他类型的请求,在 RESTful 风格的应用中,就显得非常鸡肋,因此我们推荐在后端通过 (CORS,Crossorigin resource sharing) 来解决跨域问题。这种解决方案并非 Spring Boot 特有的,在传统的SSM 框架中,就可以通过 CORS 来解决跨域问题,只不过之前我们是在 XML 文件中配置 CORS ,现在可以通过实现WebMvcConfifigurer接口然后重写addCorsMappings方法解决跨域问题。
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(3600);
}
}
33. Spring Boot 中的监视器是什么?

34. 如何使用 Spring Boot 实现全局异常处理?
- Spring 提供了一种使用 ControllerAdvice 处理异常的非常有用的方法。 我们通过实现一个ControlerAdvice 类,来处理控制器类抛出的所有异常。
35. 我们如何监视所有 Spring Boot 微服务?

36. SpringBoot性能如何优化
- 如果项目比较大,类比较多,不使用@SpringBootApplication,采用@Compoment指定扫包范围
- 在项目启动时设置JVM初始内存和最大内存相同
- 将springboot内置服务器由tomcat设置为undertow
37. 如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?Spring Boot项目如何热部署?

38. SpringBoot微服务中如何实现 session 共享 ?

39. 您使用了哪些 starter maven 依赖项?

40. Spring Boot 中的 starter 到底是什么 ?

41. Spring Boot 中如何实现定时任务 ?
- 在 Spring Boot 中使用定时任务主要有两种不同的方式,一个就是使用 Spring 中的 @Scheduled注解,另一-个则是使用第三方框架 Quartz。
- 使用 Spring 中的 @Scheduled 的方式主要通过 @Scheduled 注解来实现。
42. spring-boot-starter-parent 有什么用 ?

43. SpringBoot如何实现打包
- 进入项目目录在控制台输入mvn clean package,clean是清空已存在的项目包,package进行打包
- 或者点击左边选项栏中的Mavne,先点击clean在点击package
44.Spring Boot 打成的 jar 和普通的 jar 有什么区别 ?

45. SpringBoot的特征?
- 创建独立的Spring应用程序
- 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
- 提供固化的“starter”依赖项,以简化构建配置
- 尽可能自动配置Spring和3rd Party库
- 提供可用于生产的功能,例如指标,运行状况检查和外部化配置
- 完全没有代码生成,也不需要XML配置
46.如何快速构建一个SpringBoot项目?
- 通过Web界面使用。http://start.spring.io
- 通过Spring Tool Suite使用。
- 通过IntelliJ IDEA使用。
- 使用Spring Boot CLI使用。
47.SpringBoot启动类注解?它是由哪些注解组成?

48.什么是yaml?
- YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl。更具有结构性。
49.SpringBoot支持配置文件的格式?

50.SpringBoot启动方式?
-
- main方法
-
- 命令行 java -jar 的方式
-
- mvn/gradle
51.SpringBoot需要独立的容器运行?
- 不需要,内置了 Tomcat/Jetty。
52.SpringBoot配置途径?
- 命令行参数
- java:comp/env里的JNDI属性
- JVM系统属性
- 操作系统环境变量
- 随机生成的带random.*前缀的属性(在设置其他属性时,可以引用它们,比如${random.long})
- 应用程序以外的application.properties或者appliaction.yml文件
- 打包在应用程序内的application.properties或者appliaction.yml文件
- 通过@PropertySource标注的属性源
- 默认属性
53.application.properties和application.yml文件可放位置?优先级?

54.SpringBoot自动配置原理?
@EnableAutoConfifiguration (开启自动配置) 该注解引入了AutoConfifigurationImportSelector,该类中的方法会扫描所有存在META-INF/spring.factories的jar包。
55.SpringBoot热部署方式?
- spring-boot-devtools
- Spring Loaded
- Jrebel
- 模版热部署
56.「bootstrap.yml」 和「application.yml」?
- bootstrap.yml 优先于application.yml
57.SpringBoot如何修改端口号?

58.开启SpringBoot特性的几种方式?
- 继承spring-boot-starter-parent项目
- 导入spring-boot-dependencies项目依赖
59.SpringBoot如何兼容Spring项目?
在启动类加:
@ImportResource(locations = {"classpath:spring.xml"})
60.SpringBoot配置监控?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
61.获得Bean装配报告信息访问哪个端点?
- /beans 端点
62.关闭应用程序访问哪个端点?

63.查看发布应用信息访问哪个端点?
- /info
64.针对请求访问的几个组合注解?
- @PatchMapping
- @PostMapping
- @GetMapping
- @PutMapping
- @DeleteMapping
65.SpringBoot 中的starter?

66.SpringBoot集成Mybatis?
67.什么是SpringProfiles?
mybatis-spring-boot-starter
68.不同的环境的配置文件?

69.如何激活某个环境的配置?

70.编写测试用例的注解?
- @SpringBootTest
71.SpringBoot异常处理相关注解?
- @ControllerAdvice
- @ExceptionHandler
72.SpringBoot 1.x 和 2.x区别?
- SpringBoot 2基于Spring5和JDK8,Spring 1x用的是低版本。
- 配置变更,参数名等。
- SpringBoot2相关的插件最低版本很多都比原来高
- 2.x配置中的中文可以直接读取,不用转码
- Actuator的变化
- CacheManager 的变化
73.SpringBoot读取配置相关注解有?
- @PropertySource
- @Value
- @Environment
- @ConfifigurationProperties
最后
小编分享的文章到这里就结束了,整理不易,欢迎大家一起交流。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· DeepSeek “源神”启动!「GitHub 热点速览」
· 上周热点回顾(2.17-2.23)