Spring Boot面试题

Spring Boot面试题

基础概念

Q1: Spring Boot的核心特性有哪些?

public class SpringBootBasicDemo {// 1. 自动配置@SpringBootApplicationpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}// 自定义配置@Configurationpublic class CustomConfig {@Bean@ConditionalOnMissingBeanpublic DataSource dataSource() {return DataSourceBuilder.create().url("jdbc:mysql://localhost:3306/demo").username("root").password("password").build();}}}// 2. 外部配置@Configuration@ConfigurationProperties(prefix = "app")public class AppProperties {private String name;private String description;private Map<String, String> additionalHeaders = new HashMap<>();// getters and setters}
}

Q2: Spring Boot的启动流程是怎样的?

public class SpringBootStartupDemo {// 1. 启动流程public class StartupExample {public void startupDemo() {@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication app = new SpringApplication(Application.class);app.setBannerMode(Banner.Mode.OFF);app.setWebApplicationType(WebApplicationType.SERVLET);app.addListeners(new ApplicationListener<ApplicationEvent>() {@Overridepublic void onApplicationEvent(ApplicationEvent event) {if (event instanceof ApplicationStartedEvent) {// 应用启动后的处理}}});app.run(args);}}}}// 2. 自定义启动器public class CustomStarterExample {public void starterDemo() {// 自动配置类@Configuration@ConditionalOnClass(CustomService.class)@EnableConfigurationProperties(CustomProperties.class)public class CustomAutoConfiguration {@Autowiredprivate CustomProperties properties;@Bean@ConditionalOnMissingBeanpublic CustomService customService() {return new CustomService(properties);}}// 配置属性@ConfigurationProperties(prefix = "custom")public class CustomProperties {private String property;private boolean enabled = true;// getters and setters}}}
}

高级特性

Q3: Spring Boot的自动配置原理是怎样的?

public class AutoConfigurationDemo {// 1. 条件注解public class ConditionalExample {public void conditionalDemo() {@Configuration@ConditionalOnClass(DataSource.class)public class DatabaseConfiguration {@Bean@ConditionalOnMissingBeanpublic DataSource dataSource() {// 数据源配置}@Bean@ConditionalOnProperty(prefix = "spring.jpa",name = "enabled",havingValue = "true",matchIfMissing = true)public JpaTransactionManager transactionManager() {// 事务管理器配置}}}}// 2. 自动配置实现public class AutoConfigImplementationExample {public void autoConfigDemo() {@Configuration@EnableAutoConfiguration@ComponentScanpublic class AppConfig {@Beanpublic BeanFactoryPostProcessor beanFactoryPostProcessor() {return new BeanFactoryPostProcessor() {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// 处理bean工厂}};}}}}
}

Q4: Spring Boot的监控和运维是怎样的?

public class MonitoringDemo {// 1. Actuator配置public class ActuatorExample {public void actuatorDemo() {@Configurationpublic class ActuatorConfig {@Beanpublic HealthIndicator customHealthIndicator() {return new HealthIndicator() {@Overridepublic Health health() {return Health.up().withDetail("app", "Healthy").withDetail("error", "None").build();}};}@Beanpublic EndpointWebExtension metricsEndpointWebExtension(MetricsEndpoint metricsEndpoint) {return new EndpointWebExtension() {@ReadOperationpublic WebEndpointResponse<Map> getMetrics() {return new WebEndpointResponse<>(metricsEndpoint.metric("jvm.memory.used", null).getMeasurements().stream().collect(Collectors.toMap(Measurement::getStatistic,Measurement::getValue)),200);}};}}}}// 2. 自定义监控指标public class CustomMetricsExample {public void metricsDemo() {@Componentpublic class CustomMetricsExporter {private final MeterRegistry registry;public CustomMetricsExporter(MeterRegistry registry) {this.registry = registry;}public void recordCustomMetric(String name, double value) {registry.gauge(name, value);}public void incrementCounter(String name) {registry.counter(name).increment();}public void recordTimer(String name, long timeInMs) {registry.timer(name).record(Duration.ofMillis(timeInMs));}}}}
}

Q5: Spring Boot的性能优化有哪些?

public class PerformanceOptimizationDemo {// 1. JVM优化public class JVMOptimizationExample {public void jvmOptDemo() {// JVM参数配置public class JVMConfig {public void configureJVM() {// 设置JVM参数System.setProperty("spring.jmx.enabled", "false");System.setProperty("spring.config.location", "classpath:/application.yml");System.setProperty("server.tomcat.max-threads", "200");System.setProperty("server.tomcat.min-spare-threads", "10");}}}}// 2. 数据库优化public class DatabaseOptimizationExample {public void dbOptDemo() {@Configurationpublic class DatabaseConfig {@Beanpublic DataSource dataSource() {HikariConfig config = new HikariConfig();config.setMaximumPoolSize(10);config.setMinimumIdle(5);config.setIdleTimeout(300000);config.setConnectionTimeout(20000);config.setMaxLifetime(1200000);return new HikariDataSource(config);}@Beanpublic JdbcTemplate jdbcTemplate(DataSource dataSource) {JdbcTemplate template = new JdbcTemplate(dataSource);template.setFetchSize(100);template.setMaxRows(500);return template;}}}}// 3. 缓存优化public class CacheOptimizationExample {public void cacheOptDemo() {@Configuration@EnableCachingpublic class CacheConfig {@Beanpublic CacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager("users");cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(60, TimeUnit.MINUTES).maximumSize(100));return cacheManager;}}@Servicepublic class UserService {@Cacheable(value = "users", key = "#id")public User getUser(Long id) {// 从数据库获取用户return userRepository.findById(id).orElse(null);}@CacheEvict(value = "users", key = "#user.id")public void updateUser(User user) {// 更新用户userRepository.save(user);}}}}
}

面试关键点

  1. 理解Spring Boot的核心特性
  2. 掌握启动流程和原理
  3. 熟悉自动配置机制
  4. 了解监控和运维
  5. 掌握性能优化方法
  6. 理解配置管理
  7. 注意安全实践
  8. 关注最佳实践

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/23875.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

轻量级SDK,大能量:EasyRTC重塑嵌入式设备音视频体验

在智能硬件与物联网迅猛发展的今天&#xff0c;嵌入式设备的音视频通讯能力正变得愈加关键。然而&#xff0c;受限于硬件资源&#xff0c;尤其是Flash存储空间的不足&#xff0c;传统的音视频通讯方案往往难以在嵌入式设备上实现高效集成。EasyRTC凭借其轻量级SDK和先进的技术架…

算法日常刷题笔记(2)

为保持刷题的习惯 计划一天刷3-5题 然后一周总计汇总一下 这是第二篇笔记 笔记时间为2月17日到2月23日 第一天 找到初始输入字符串 找到初始输入字符串 Ihttps://leetcode.cn/problems/find-the-original-typed-string-i/ Alice 正在她的电脑上输入一个字符串。但是她打字技…

[实现Rpc] 客户端 | Requestor | RpcCaller的设计实现

目录 Requestor类的实现 框架 完善 onResponse处理回复 完整代码 RpcCaller类的实现 1. 同步调用 call 2. 异步调用 call 3. 回调调用 call Requestor类的实现 &#xff08;1&#xff09;主要功能&#xff1a; 客户端发送请求的功能&#xff0c;进行请求描述对服务器…

WPS计算机二级•文档的页面设置与打印

听说这是目录哦 纸张大小页边距和装订线❤️‍&#x1f525;打印界面讲解❤️缩印&#x1f495;打印作文稿纸&#x1f49e;将文档打印成书籍&#x1f493;限制编辑设置&#x1f497;给文字文档加密&#x1f496;文档导出为 PDF格式&#x1f498;协作编辑模式&#x1f49d;能量站…

hackmyvm-buster

题目地址 信息收集 主机发现 ┌──(root㉿kali)-[/home/kali] └─# arp-scan -I eth1 192.168.56.0/24 Interface: eth1, type: EN10MB, MAC: 00:0c:29:34:da:f5, IPv4: 192.168.56.103 WARNING: Cannot open MAC/Vendor file ieee-oui.txt: Permission denied WARNING: C…

【入门音视频】音视频基础知识

&#x1f308;前言&#x1f308; 这个系列在我学习过程中&#xff0c;对音视频知识归纳总结的笔记。因为音视频相关讲解非常稀少&#xff0c;所以我希望通过这个音视频系列&#xff0c;跟大家一起学习音视频&#xff0c;希望减少初学者在学习上的压力。同时希望也欢迎指出文章的…

将Ubuntu操作系统的安装源设置为阿里云

在使用Ubuntu操作系统时,默认的软件源通常是国外的仓库,这可能会导致软件安装和更新速度较慢。为了提高下载速度和稳定性,我们可以将Ubuntu的安装源设置为阿里云镜像源。以下是详细步骤: 一、准备工作 在开始之前,请确保您的Ubuntu系统可以正常上网,并且您拥有管理员权…

基于 Python 的项目管理系统开发

基于 Python 的项目管理系统开发 一、引言 在当今快节奏的工作环境中&#xff0c;有效的项目管理对于项目的成功至关重要。借助信息技术手段开发项目管理系统&#xff0c;能够显著提升项目管理的效率和质量。Python 作为一种功能强大、易于学习且具有丰富库支持的编程语言&…

LabVIEW C编译支持工具库CCompileSupp.llb

路径&#xff1a;C:\Program Files (x86)\National Instruments\LabVIEW 2019\vi.lib\Platform\CCompileSupp.llb ​ 1. 工具库概述 定位&#xff1a;LabVIEW内置的C语言编译支持工具库&#xff0c;用于处理LabVIEW与C/C代码的混合编程接口&#xff0c;涵盖编译器配置、代码生成…

JVM之JVM的组成

Java 虚拟机&#xff08;JVM&#xff09;是 Java 程序的运行核心&#xff0c;它主要由类加载系统、运行时数据区、执行引擎和本地方法接口这几个关键部分组成。 类加载系统&#xff08;Class Loading System&#xff09; 类加载系统负责在程序运行时动态地将 Java 类加载到 J…

pycharm 调试 debug 进入 remote_sources

解决办法1&#xff1a; pycharm函数跳转到remote_sources中的文件中_pycharm修改remotesource包存放地址-CSDN博客 file->settings->project structure将项目文件夹设为"Sources"&#xff08;此时文件夹会变为蓝色&#xff09;。 解决方法2 Debug:使用Pychar…

iOS App的启动与优化

App的启动流程 App启动分为冷启动和热启动 冷启动&#xff1a;从0开始启动App热启动&#xff1a;App已经在内存中&#xff0c;但是后台还挂着&#xff0c;再次点击图标启动App。 一般对App启动的优化都是针对冷启动。 App冷启动可分为三个阶段&#xff1a; dyld&#xff1a…

StarRocks FE leader节点CPU使用率周期性的忽高忽低问题分析

背景 本文基于 StarRocks 3.3.5 最近在做一些 StarRocks 相关的指标监控的时候&#xff0c;看到了FE master的CPU使用率相对其他FE节点是比较高的&#xff0c;且 呈现周期性的变化&#xff08;周期为8分钟&#xff09;&#xff0c; 于此同时FE master节点的GC频率相对于其他节…

Spring高级篇-Spring IOC容器 Aware 接口

一、概述 在Spring框架中&#xff0c;IOC&#xff08;Inversion of Control&#xff09;容器负责管理应用程序中的对象&#xff08;即Bean&#xff09;的生命周期和依赖关系。Spring提供了一系列的Aware接口&#xff0c;允许Bean在初始化时获取Spring容器中的某些资源或信息。…

数字信任的底层逻辑:密码学核心技术与现实应用

安全和密码学 --The Missing Semester of Your CS Education 目录 熵与密码强度密码散列函数密钥体系 3.1 对称加密 3.2 非对称加密信任模型对比典型应用案例安全实践建议扩展练习杂项 密码学是构建数字信任的基石。 本文浅析密码学在现实工具中的应用&#xff0c;涵盖 1&…

MySQL数据库连接池泄露导致MySQL Server超时关闭连接

前言 最近做项目&#xff0c;发现老项目出现xxx&#xff0c;这个错误其实很简单&#xff0c;出现在MySQL数据库Server端对长时间没有使用的client连接执行清楚处理&#xff0c;因为是druid数据库&#xff0c;且在github也出现这样的issue&#xff1a;The last packet successf…

DirectX12(D3D12)基础教程三 线性代数与3D世界空间

线性代数是数学的一个分支&#xff0c;它的研究对象是向量&#xff0c;向量空间&#xff08;或称线性空间&#xff09;&#xff0c;线性变换和有限维的线性方程组。 向量和矩阵是学习3D入门最基本的理论基础。本章重点讲向量和矩阵. 向量概念 向量最基本的定义就是一个方向和…

LeetCode 230.二叉搜索树中第K小的元素

题目&#xff1a;给定一个二叉搜索树的根节点 root &#xff0c;和一个整数 k &#xff0c;请你设计一个算法查找其中第 k 小的元素&#xff08;从 1 开始计数&#xff09;。 思路&#xff1a; 代码&#xff1a; /*** Definition for a binary tree node.* public class Tre…

Android 老项目 jcenter 库失效

最近重新维护了一些老项目发现大部分jcenter库失效了&#xff0c; Could not resolve com.xx:2.1.3. 如果你也遇到了&#xff0c;不妨试试 替换为 aliyun的jcenter服务&#xff0c;就不用一个个找代替库了。 project 下的 build.gradle 文件添加&#xff1a; maven { url htt…

Python数据结构:哈希表-高效存储与查找的秘密武器!

大家周一好&#xff01;今天我们来聊聊Python中一个非常重要的数据结构——哈希表。无论是算法面试还是实际开发&#xff0c;哈希表都扮演着至关重要的角色。掌握它&#xff0c;你就能轻松解决许多复杂的编程问题&#xff01; 在编程中&#xff0c;如何实现快速的存储与查找操…