7.SpringBoot面试题

  • 1. SpringBoot自动配置原理是什么?
  • 2.开启SpringBoot特性有哪几种方式?
  • 3.SpringBoot如何解决跨域问题
  • 4.SpringBoot如何整合Mybatis
  • 5.SpringBoot怎么实现多环境配置?
  • 6.SpringBoot的核心注解有哪些?
  • 7.SpringBoot中静态首页或者静态资源默认位置可以放在哪里?
  • 8.SpringBoot的核心配置文件有哪几个?他们的区别是什么?
  • 9.SpringBoot的优点有哪些?

1. SpringBoot自动配置原理是什么?

SpringBoot的项目启动注解是@SpringBootApplication,其实就是由下面三个注解组成的

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

其中@EnableAutoConfiguration是实现自动配置的入口,该注解又通过@Import注解导入了AutoConfigurationImportSelector,该类会加载META-INF/spring.factories的配置信息。然后根据@Condition条件来筛选出有效的自动配置类。每一个自动配置类结合对应的xxxProperties.java读取配置文件进行自动配置功能。

 

2.开启SpringBoot特性有哪几种方式?

有以下两种方式:
继承spring-boot-starter-parent项目

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

导入spring-boot-dependencies项目依赖

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

 

3.SpringBoot如何解决跨域问题

方法一:使用@CrossOrigin注解
在Spring Boot 中给我们提供了一个注解 @CrossOrigin 来实现跨域,这个注解可以实现方法级别的细粒度的跨域控制。我们可以在类或者方法添加该注解,如果在类上添加该注解,该类下的所有接口都可以通过跨域访问,如果在方法上添加注解,那么仅仅只限于加注解的方法可以访问。

@RestController
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryMapper categoryMapper;
    @RequestMapping("/category")
    public List<Category> findAll(){
        return categoryMapper.selectAll();
    }
}

方法二:实现WebMvcConfigurer
这种解决方案并非 Spring Boot 特有的,在传统的 SSM 框架中,就可以通过 CORS 来解决跨域问题,只不过之前我们是在 XML 文件中配置 CORS ,现在可以通过实现WebMvcConfigurer接口然后重写addCorsMappings方法解决跨域问题。

@Configuration
public class MyWebConfigurer implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("*") //支持所有请求方法跨域
                .allowedHeaders("*") //支持所有请求头跨域
                .allowCredentials(true) //跨域请求可以包含cookie
                .allowedOriginPatterns("*")//允许任何域名使用,低版本Springboot用allowedOrigins()方法
                .maxAge(3600 * 24);
    }
}

addMapping:配置可以被跨域的路径,可以任意配置,可以具体到直接请求路径。

allowedOriginPatterns:允许所有的请求域名访问我们的跨域资源,可以固定单条或者多条内容,如:“http://www.baidu”,只有百度可以访问我们的跨域资源。低版本springboot需要使用allowedOrigins

allowCredentials: 跨域请求是否允许包含Cookie。

allowedMethods:允许输入参数的请求方法访问该跨域资源服务器,如:POST、GET、PUT、OPTIONS、DELETE等。

allowedHeaders:允许所有的请求header访问,可以自定义设置任意请求头信息,如:“X-YAUTH-TOKEN”。

maxAge:配置预检请求的有效时间(以秒为单位)。默认设置为1800秒(30分钟)。

方法三:使用CorsFilter过滤器

@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}

 

4.SpringBoot如何整合Mybatis

导入Mybatis和数据库驱动的依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

application.properties配置数据源和mapper.xml文件的映射路径

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://8.140.19.47:3306/gulimall_pms?useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml

主启动类添加@MapperScan注解,添加mapper接口的扫描路径

@SpringBootApplication
@MapperScan("com.springboot.springbootdemo.dao")
public class SpringbootdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

 

5.SpringBoot怎么实现多环境配置?

profile是用来完成不同环境下,配置动态切换功能的

多profile文件方式:提供多个配置文件,每个代表一种环境。
application-dev.properties/yml 开发环境
application-test.properties/yml 测试环境
application-pro.properties/yml 生产环境

profile激活方式
配置文件:spring.profiles.active=dev
 

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

启动类上面的注解@SpringBootApplication是SpringBoot的核心注解,主要包含3个注解
(1)@SpringBootConfiguration(里面还包含一个@Configuration),表示当前类为SpringBoot配置类
(2)@EnableAutoConfiguration:开启自动配置的功能,可以单独关闭某些资源的自动配置
(3)@ComponentScan:扫描组件的功能,默认是扫描主启动类所在包以及子包的所有组件。

7.SpringBoot中静态首页或者静态资源默认位置可以放在哪里?

classpath: resources/index.html
classpath: static/index.html
classpath: public/index.html
classpath: META-INF/resources/index.html
优先级 META-INF/resources/>resources/>static/>public/

8.SpringBoot的核心配置文件有哪几个?他们的区别是什么?

SpringBoot的核心配置文件是application和bootstrap配置文件。
application配置文件这个容易理解,主要用于Spring Boot项目的自动化配置。
bootstrap里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。

bootstrap配置文件有以下几个应用场景:

  • 使用Spring Cloud Config配置中心时,这时需要在bootstrap配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;
  • 一些固定的不能被覆盖的属性;
  • 一些加密/解密的场景
     

9.SpringBoot的优点有哪些?

(1)SpringBoot通过提供一个插件spring-boot-maven-plugin能够将Web应用程序打包为可执行的JAR,简化了部署。
(2)内嵌servlet容器(可以选择内嵌: tomcat ,jetty等服务器)。
(3)提供了starter的pom配置,简化了依赖。
(4)自动配置spring容器中的bean。当不满足实际开发场景,还可以自定义bean的自动化配置。
(5)springboot无需做出xml配置,基本上都是使用注解和Java Config,简化了配置。
(6)springboot还简化了监控,我们可以引入spring-boot-start-actuator依赖,直接使用REST方式来获取进程的运行期性能参数,从而达到监控的目的,比较方便。

更多推荐

7.SpringBoot面试题