(翻译)Spring MVC 面试题

1. 简介

Spring MVC是Spring公司在Servlet API基础上建立的原创Web框架。它提供了模型-视图-控制器架构,可用于开发灵活的Web应用。

在本教程中,我们将重点讨论与之相关的问题,因为它经常是Spring开发者求职面试的一个话题。

关于Spring框架的更多问题,你可以查看我们面试问题系列中另一篇与Spring有关的文章

2.基础 Spring MVC 题

Q1. 为什么我们要使用Spring MVC?

Spring MVC实现了清晰的关注点分离,使我们能够轻松开发和单元测试我们的应用程序。

像如下概念:

  • Dispatcher Servlet
  • Controllers
  • View Resolvers
  • Views, Models
  • ModelAndView
  • Model and Session Attributes

是完全相互独立的,它们只负责一件事。

因此,MVC给了我们相当大的灵活性。它是基于接口的(有提供的实现类),我们可以通过使用自定义接口来配置框架的每一部分。

另一件重要的事情是,我们并没有被束缚在一个特定的视图技术上(例如JSP),而是可以选择我们最喜欢的技术。

另外,我们不只在Web应用开发中使用Spring MVC,在创建RESTful Web服务时也是如此。

继续阅读

(翻译)常见 Spring 框架面试题

1.概述

在本教程中,我们将看看在求职面试中可能出现的一些最常见的与 Spring 有关的问题。

2. Spring Core

Q1. 什么是 Spring 框架?

Spring是开发 Java 企业版应用程序最广泛使用的框架。此外,Spring 的核心功能可用于开发任何 Java 应用程序。

我们使用它的扩展功能在 Jakarta EE 平台之上构建各种网络应用。我们也可以在简单的独立应用程序中使用它的依赖注入功能。

Q2. 使用 Spring 的好处是什么?

Spring 的目标是使 Jakarta EE的 开发更容易,所以我们来看看它的好处:

  • 轻量级–在开发中使用该框架的开销很小
  • 反转控制(IoC)–Spring 容器负责连接各种对象的依赖关系,而不是创建或寻找依赖对象
  • 面向切面编程(AOP)–Spring支持AOP,将业务逻辑与系统服务分开
  • IoC容器–管理 Spring Bean 的生命周期和项目特定的配置
  • MVC框架–用于创建 Web 应用程序或 RESTful Web服务,能够返回XML/JSON响应
  • 事务管理 – 通过使用 Java 注解或 Spring Bean 的 XML 配置文件,减少JDBC操作、文件上传等方面的模板代码量
  • 异常处理–Spring 提供了一个方便的 API,用于将特定技术的异常转化为未检查的异常

Q3. 您知道哪些 Spring 子项目?简要描述一下吧。

  • 核心–提供框架基础部分的关键模块,如 IoC 或 DI
  • JDBC–实现了一个 JDBC 抽象层,不需要为特定的供应商数据库进行 JDBC 编码
  • ORM 集成 – 为流行的对象关系映射 API 提供集成层,如 JPA、JDO 和 Hibernate
  • Web–一个面向网络的集成模块,提供多部分文件上传、Servlet监听器和面向网络的应用程序上下文功能
  • MVC 框架–一个实现模型-视图-控制器设计模式的 Web 模块
  • AOP 模块–面向切面编程实现,允许定义干净的方法拦截器和切点
继续阅读

CentOS 8 迁移到 Rocky Linux 时遇到的问题

虽然 Rocky Linux 专门写了一篇文档介绍如何从 CentOS 8 迁移过来,但实际迁移还是遇到了问题,这里简单记一下。

1
2
3
4
5
6
7
8
9
10
Error: 
 Problem 1: cannot install both ibus-libs-1.5.19-14.el8_5.x86_64 and ibus-libs-1.5.19-12.el8.x86_64
  - package ibus-devel-1.5.19-12.el8.x86_64 requires ibus-libs(x86-64) = 1.5.19-12.el8, but none of the providers can be installed
  - cannot install the best update candidate for package ibus-libs-1.5.19-12.el8.x86_64
  - problem with installed package ibus-devel-1.5.19-12.el8.x86_64
 Problem 2: cannot install both marisa-0.2.4-36.el8.x86_64 and marisa-0.2.4-4.el7.x86_64
  - package marisa-devel-0.2.4-4.el7.x86_64 requires marisa(x86-64) = 0.2.4-4.el7, but none of the providers can be installed
  - cannot install the best update candidate for package marisa-0.2.4-4.el7.x86_64
  - problem with installed package marisa-devel-0.2.4-4.el7.x86_64
(try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)

第一个问题是升级 ibus-devel 失败,这是因为 ibus-devel 在 PowerTools 的仓库中,默认可能没有使能这个仓库,我们可以手动使能安装或升级:sudo dnf --enablerepo=powertools install ibus-devel

第二个问题是 CentOS 8 上有最新的 marisa-0.2.4-36.el8.x86_64,但是只有marisa-devel-0.2.4-4.el7.x86_64, 这个有点奇怪,不知道为什么两个版本没有同步升级,marisa-devel 一般是用于开发,我们可能暂时用不到,可以先尝试卸载它完成升级:sudo dnf remove marisa-devel

1
2
3
4
5
6
7
8
9
10
11
12
13
Syncing packages

Last metadata expiration check: 0:00:13 ago on Tue Mar  8 12:23:04 2022.

Error: 
 Problem: package kyotocabinet-1.2.77-1.el7.x86_64 requires kyotocabinet-libs(x86-64) = 1.2.77-1.el7, but none of the providers can be installed
  - kyotocabinet-libs-1.2.77-1.el7.x86_64 does not belong to a distupgrade repository
  - problem with installed package kyotocabinet-1.2.77-1.el7.x86_64
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)

Error during distro-sync.

An error occurred while we were attempting to convert your system to Rocky Linux. Your system may be unstable. Script will now exit to prevent possible damage.

第三个问题是 kyotocabinet 的版本问题导致 distro-sync 失败。我查询了一下系统中安装的 kyotocabinet 版本又确实是 1.2.77-1.el7,也可以查询到迁移成功了:

1
2
3
4
5
6
7
8
9
10
11
$ hostnamectl 
   Static hostname: centos.tenneshop.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2af6da6bd3624e0988bd30e22574b645
           Boot ID: 6edac64aba294e1e9f3b7cfdb7b6970c
    Virtualization: oracle
  Operating System: Rocky Linux 8.5 (Green Obsidian)
       CPE OS Name: cpe:/o:rocky:rocky:8:GA
            Kernel: Linux 4.18.0-240.15.1.el8_3.x86_64
      Architecture: x86-64

通过 Syncing packages 搜索 migrate2rocky 脚本,这已经是最后三步了,于是尝试手动完成剩下的工作。 先 dnf --allowerasing distro-sync,然后查看会删除哪些软件,我这里是会删除 kyotocabinet,根据情况确认是否接受删除软件同步。

最后的 Disable Stream repos 和移除 subscription-manage,因为是 CentOS 8, 所以并不需要。

重启之后又遇到新的问题:

1
vboxclient the virtualbox kernel service is not running. exiting

查看 /var/log/vboxadd-setup.log

1
2
3
4
5
6
7
8
/tmp/vbox.0/r0drv/linux/alloc-r0drv-linux.c:204:14: error: implicit declaration of function ‘map_vm_area’; did you mean ‘get_vm_area’? [-Werror=implicit-function-declaration]
         if (!map_vm_area(pVmArea, PAGE_KERNEL_EXEC,
              ^~~~~~~~~~~
              get_vm_area
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:316: /tmp/vbox.0/combined-os-specific.o] Error 1
make[1]: *** [Makefile:1571: _module_/tmp/vbox.0] Error 2
make: *** [/tmp/vbox.0/Makefile-footer.gmk:117: vboxguest] Error 2

经过一番搜索,我意识到可能是 VBoxGuestAdditions.iso 的版本不对,因为之前确实有提示类似 unable to insert the virtual optical disk /usr/share/virtualbox/vboxguestadditions.iso 的错误,原因是我没有把之前版本的 VBoxGuestAdditions.iso 从虚拟光驱中弹出,于是先弹出再插入新版本,果然新版本成功安装。

Reference: