21 Common Spring Boot Interview Questions

Ordinary programmer 2022-09-23 07:47:34 阅读数:960


在过去的几个月里,I received some aboutSpring BootThe request of the interview questions.在这篇文章中,我将涵盖21个Spring Boot面试问题.此外,I will also introduce some problems related to micro service architecture.

我将这些Spring BootInterview questions are divided into three categories——针对新手、Intermediate and experienced developers.

1. Spring Boot是什么?它在WebWhat good is a application development?

Spring Boot是一个构建在Spring framework之上的框架,用于快速应用程序开发.同样,Ruby on Rails、Django与Python的webFramework is also very famous.特别是,Spring为JavaBrought a much-neededweb框架来构建web应用程序.The embedded application server and the important features such as automatic configuration allows rapid development and deployment.

  • Spring BootFramework allows to easily import dependency.
  • It also by its rich features simplified use in your applicationSpring Security.
  • Actuator-Can realize monitoring application.

2. Spring Boot和 Spring有什么区别?

SpringFramework includes inSpring BootFramework for use in a variety of features.另一方面,Spring BootFramework for application development.

同样,Spring Framework provides the data binding、依赖注入、面向方面编程、数据访问、安全性等特性.简而言之,Spring Boot是SpringOne of the framework of the module.Spring Boot构建在SpringThe top of the frame.Spring Boot是一个基于微服务的框架,用于构建应用程序.Spring BootHelps reduce the automatic configuration characteristics of lines of code.

3.如何使用maven或gradle设置Spring Boot应用程序?

要设置spring boot应用程序,可以使用maven或gradle.Both are used to pull in the application required dependencies.特别是,对于spring boot应用程序,可以拉取spring-boot-starter-parent依赖关系.

在maven中,我们使用pom.xml文件,而在gradle中,我们使用build.gradleUsed to rely on the statement.




compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.4.1'

4. 如何更改Spring Boot应用程序的默认端口?

There are three ways to changeSpring Boot应用程序的默认端口.最直接的方法是使用application.properties 或application.yml文件.

  • server.port在application.properties中使用此属性.
  • Can also be in the [email protected] to programmatically change the port.
  • 在作为jarFile to start the application,Can use the selected port.

5. 如何使用Spring Boot设置一个带有HTTPS端口的应用程序?

默认情况下,Spring Boot应用程序运行在HTTP端口上.Make sure your application USESHTTPS端口运行,有两种方法.可以使用server.port,server.ssl.key-store-password server.ssl.key-store,server.ssl.key-store-type 和server.ssl.key-alias.另一种使用HTTPSPort is programmatically.

6. Spring Boot支持哪些嵌入式服务器?

Spring Boot应用程序支持Tomcat、Jetty和Undertow网络服务器.默认情况下,Spring Boot的web启动器支持Tomcat web服务器.

要更改默认的web服务器,必须排除spring-boot-starter-tomcat,And include the corresponding server startup procedure,如jetty或undertow.​​​​​​


7. Spring Boot有什么优点?

下面是Spring Boot的主要优点:

  • Allows you to create an independentSpring应用程序
  • 可以轻松地使用tomcat、jetty或undertow服务器
  • 快速发展
  • Also allows you to easily use architecture based on micro service

到目前为止,I have already covered some of the basic interview questions.If you are aJava或Spring BootDevelopment has several years experience of developers,Where is the following a set of questions will help you improve.

8. 如何将Spring Boot Web应用程序部署为jar和war文件?

通常,我们将web应用程序部署为war.我们将warFile deployed to externalweb服务器上.

显然,如果使用maven构建应用程序,SpringWill provide a pluginSpring -boot-maven-plugin.然后,你可以将web应用程序打包为可执行的jar文件.如果使用gradle, gradle buildThe default will create an executablejar文件.

maven通过<packaging>jar</packaging> 将构建一个jar文件.<packaging>war</packaging>将构建一个war文件.If does not contain the packing element,则maven将默认生成jar文件.You also need to keep container dependencies as follows


对于gradle,You need to be embedded container dependencies marked as belong towar插件providedRuntime 配置.

9. 为什么我们在Spring Boot应用程序main Use comments in [email protected] ?

To use automatic configuration、The component scanning and additional configuration application,Can contain a [email protected] annotation includes three [email protected]、@ComponentScan和@Configuration.

  • @EnableAutoConfiguration -启用Spring Boot的自动配置
  • @ComponentScan -This allows the scanning application in package.
  • @Configuration——允许在上下文中注册额外的bean或导入额外的配置类.

10. @RestController和@RequestMapping注释用于什么?

@RestController——The comment added [email protected]和@ResponseBody.The comments mark a will handle the incoming request class.通常,This annotation is used to createRESTful api.

@RequestMapping——The annotation in dealing with the incoming request routing information supplied in the class.This annotation will be introduced to theHTTPThe request is mapped to the corresponding method.

11. 为什么我们需要Spring Porfiles?

In building enterprise applications,We use different environment,如开发、测试、QA和生产.Spring BootAllows different application for each environment configuration properties.

In order to help isolate each environment configuration,可以根据环境(如application-dev)Named after the properties file.例如:application-dev.properties,application-test.properties, and application-prod.properties.

12. 如何禁用特定的自动配置?

To disable any specific automatic configuration,可以使用@EnableAutoConfiguration注释的exclude属性.​​​​​​

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class SampleApplication

13. 什么是Thymeleaf,如何使用它?

Thymeleaf是一个webApplication of server-side templating engine.Its main purpose is to introduce the templateweb应用程序.

具体来说,要使用Thymeleaf,Must rely on contained in the applicationspring-boot-start -thymeleaf.

14. 有哪些不同的Spring Boot启动器,它们的用途是什么?

强调的是,Spring BootOne of the main advantage is able toSpring BootApplication USES a variety of dependency.

All starterSpringGuide the dependencies areorg.springframework下.这些依赖从spring-boot-starter-作为前缀.

有超过50Initial dependency,But there is a list of,Most developers use in their applications:

  • spring-boot-starter-核心启动器.This includes automatic configuration、日志记录和YAML.
  • spring-boot-starter-data-jpa——这包括使用hibernate的Spring data JPA.
  • spring-boot-starter- security——SpringSecurity model for youwebApplication provide security.默认情况下,It will add basic authentication based on the form.
  • spring-boot-start -web——这包括一个web模块,帮助使用Spring MVC创建web的RESTful应用程序.
  • spring-boot-starter-thymeleaf ——Import this module allows developers to use in the application ofthymeleaf模板引擎.
  • spring-boot-starter-jdbc——This module is to help connect the application of the database.
  • spring-boot-starter-tomcat——通常,这是spring bootPart of the parent module.This allows developers to use embeddedtomcat servlet容器.

In view of the experienced developersSpring Boot面试问题

The next set of problems is for experienced developers.尽管有这些区别,Beginner and intermediate developer should understand these problems.

15. 如何使用JPA将Spring BootThe application to connect to the database?

Spring Boot提供了spring - boot -starter-data-jpaDependency to connectSpringApplication to relational database.

另一方面,Spring Data JPATreatment based onjdbcThe complexity of the database access and object relational modeling.因此,It improved the implementation of the data access layer processing.从开发人员的角度来看,It reduces the dependence on relational database query.

JPAAllows the application class is mapped to a database table.

16. 为什么要使用 Spring Boot Actuator?

Spring Boot ActuatorFor the application can be used in the production of features.因此,Can be easily monitoring application.

It also provides for the application audit、Running status inspection and different measure.因此,You need to include the following dependencies:


The following are some of the key depends on for the application for the:

env -Show all environmental properties

health-According to the application of operation condition

metrics——According to the application of all kinds of measure

loggers——According to the application log configuration


17. Spring Boot Devtools的用途是什么?

Spring Boot DevTools或Developer ToolsIs a set of make it easier for the development process of tools.要包含DevTools支持,Must be added in the projectspring-boot-devtools依赖项.

Spring Boot DevToolsCan help disable manySpring BootDependency using cache.这在开发过程中非常有用,Because developers hope to see the changes immediately.

此外,Use this library allows applications to files on the classpath changes to restart.该库还支持webThe debug logging.

18. 使用Spring Data JPA的优势是什么?

Spring Data Jpa提供了JpaTemplate来集成Spring应用程序.还可以选择JPA规范的特定实现.


Spring Data JPA的优点:

  • 减少样板代码.
  • Generated by the query help reduce developers dependence on database query.
  • Repository Model allows developers to easily handle persistence.

19. Spring BootSupport to relax binding.

通常,Properties of key needs andSpring BootIn the application properties of exact match.Spring BootSupport to relax binding,This means that the properties of key don't have to be totally matched with the attribute name.

此外,Environmental attributes can be written in any case.例如——如果你的bean类中有一个属性propertyDB,And with a [email protected],So you can use this property to bind to any one of these environmental properties—PROPERTYDB、propertyDB或property_db.

20.如果想在应用程序中使用Spring Security,该如何使用它?How does it make application security?

目前,为了保护Spring Boot应用程序的安全,Can the project containsspring-boot-starter-security依赖项.默认情况下,It will be for your application to add some security measures.These measures include by default to the application to add basic form-based authentication or protectREST api.

随后,为了利用Spring Security,可以扩展WebSecurityConfigurerAdapterClass to add custom security measures.在同一个类中,你将使用@EnableWebSecurity注释.This annotation allowsSpringFind configuration and apply class to globalWebSecurity.

21. You use [email protected]做什么?

@ControllerAdviceComments help manageSpring BootThe application of abnormal.简而言之,它是一个拦截器,Interception by the [email protected] the exception thrown comments.

ResponseEntityExceptionHandlerThe base class is to provide centralized exception handling.

到目前为止,I have coveredSpring Boot面试的问题.


copyright:author[Ordinary programmer],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230625447903.html