← 返回首页
Springboot高频面试题(一)
发表时间:2022-08-23 13:26:23
Springboot高频面试题(一)

1.Spring Boot 有哪些优点? - 快速开发。 - 快速整合。 - 配置简化。 - 内嵌服务容器。

2.Springboot与SpringCloud的区别?

Springboot是用来开发单个微服务的模块,SpringCloud就是微服务架构的一站式解决方案,简单说就是微服务的大管家。

3.Spring Boot 有哪几种运行方式? - 直接执行启动类的main方法 - 打成jar包或者war包执行 - 用 Maven/ Gradle 插件运行

4.Spring Boot支持哪些内嵌式服务器?

我们以 2.2.6.RELEASE版本的Spring Boot为例,其支持三种内嵌式服务器:tomcat、jetty和undertow。

Spring Boot中默认使用tomcat作为内嵌的服务器。spring-boot-starter-web中已经包含了spring-boot-starter-tomcat的依赖。

三种内置服务性能对比:undertow >jetty > tomcat

5.Spring Boot 的配置文件有哪几种格式?

properties 和yml,它们的区别主要是书写格式不同。

1).properties

app.user.name = javastack

2).yml

app:
  user:
    name: javastack

注意.yml 格式不支持@PropertySource注解导入配置。想想就明白,@PropertySource本身就是针对properties资源文件的注解。

6.开启Spring Boot特性有哪几种方式?

继承spring-boot-starter-parent项目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.7.RELEASE</version>
</parent>

导入spring-boot-dependencies项目依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

7.Spring Boot如何实现热部署?

SpringBoot实现热部署其实很容易,引入devtools依赖即可。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
</dependency>

8.SpringBoot的核心注解有哪些?

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
 //略
}

9.SpringBoot中如何解决跨域问题? 通过 CORS 来解决跨域问题。实现WebMvcConfigurer接口然后重写addCorsMappings方法解决跨域问题。

@Configuration
public class CorsConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            //重写父类提供的跨域请求处理的接口
            public void addCorsMappings(CorsRegistry registry) {
                //添加映射路径
                registry.addMapping("/**")
                        //放行哪些原始域
                        .allowedOrigins("*")
                        //是否发送Cookie信息
                        .allowCredentials(true)
                        //放行哪些原始域(请求方式)
                        .allowedMethods("*")
                        //放行哪些原始域(头部信息)
                        .allowedHeaders("*")
                        //暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
                        .exposedHeaders("token");
            }
        };
    }
}

需要注意:Springboot升级到2.4.0版本后,针对CorsConfiguration新增了效验,原来的allowedOrigin改为allowedOriginPattern即可。

@Configuration
public class CorsConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            //重写父类提供的跨域请求处理的接口
            public void addCorsMappings(CorsRegistry registry) {
                //添加映射路径
                registry.addMapping("/**")
                        //放行哪些原始域
                        .allowedOriginPatterns("*")
                        //是否发送Cookie信息
                        .allowCredentials(true)
                        //放行哪些原始域(请求方式)
                        .allowedMethods("*")
                        //放行哪些原始域(头部信息)
                        .allowedHeaders("*")
                        //暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
                        .exposedHeaders("token");
            }
        };
    }
}

10.SpringBoot中如何实现定时任务?

在 Spring Boot 中使用定时任务主要有两种不同的方式,一个就是使用 Spring 中的 @Scheduled 注解,另一个则是使用第三方框架 Quartz。

使用Spring 中的 @Scheduled 的方式主要通过 @Scheduled 注解来实现。 使用Quartz,则按照Quartz的方式,定义Job和Trigger即可。