
微信扫一扫下面小程序码,进入面试一点通,收录全网面试题

开心一些
springboot面试大概率会问到的问题:
什么是Spring Boot?
Spring Boot与Spring cloud的区别
Spring Boot有哪些特性
Spring Boot配置文件的优先级
Spring Boot是spring的脚手架,使用Spring Boot可以做到专注于Spring应用的开发,而无需过多关注XML的配置。能够使开发者在极短的时间内开发出单个微服务。Spring Boot使用“习惯优于配置”的理念,简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题。使用Spring Boot可以不用或者只需要很少的Spring配置就可以让企业项目快速运行起来。
Spring Boot的核心功能:
可独立运行的Spring项目:Spring Boot可以以jar包的形式独立运行。
内嵌的Servlet容器:Spring Boot可以选择内嵌Tomcat、Jetty或者Undertow,无须以war包形式部署项目。
简化的Maven配置:Spring提供推荐的基础 POM 文件来简化Maven 配置。
自动配置Spring:Spring Boot会根据项目依赖来自动配置Spring 框架,极大地减少项目要使用的配置。
提供生产就绪型功能:提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。
无代码生成和xml配置:Spring Boot不生成代码。完全不需要任何xml配置即可实现Spring的所有配置。
Spring boot是Spring的一套快速配置脚手架,可以基于Spring Boot快速开发单个微服务;Spring Cloud是一个基于Spring Boot实现的云应用开发工具
Spring Boot专注于快速、方便集成的单个个体,Spring Cloud是关注全局的服务治理框架;
Spring boot使用了默认大于配置的理念,很多集成方案已经帮你选择好了,能不配置就不配置,Spring Cloud很大的一部分是基于Spring Boot来实现。
Spring Boot可以离开Spring Cloud独立使用开发项目,但是Spring Cloud离不开Spring Boot,属于依赖的关系。
这个问题就很容易被问到了,Spring Boot主要有两大特性,分别是:起步依赖和自动装配,接下来就仔细说一下:
起步依赖:
每一个Spring Boot项目都必须继承依赖:spring-boot-starter-parent,而点进这个依赖能看到,它又继承了spring-boot-dependencies依赖,如下图:

我的项目pom文件

spring-boot-starter-parent的pom文件
而在spring-boot-dependencies的pom文件中,有大量的依赖,以及定义好的每个依赖的版本号,这里的版本号,是根据springboot的版本号而定义的,不同版本的springboot版本,各个依赖的版本号也不一致的:
这里即解决了我们原有项目中可能存在依赖版本冲突的问题,它来真正管理spring boot应用里面的所有依赖版本。

除了继承父依赖之外,我们的项目还需要引入spring-boot-starter-web依赖,它帮我们导入了web模块正常运行所依赖的组件,这些依赖的版本则由父项目进行管理。而这个里面也引用了tomcat的依赖,我们可以通过自动装配的tomcat端口启动项目。


引用的依赖
总结:
在起步依赖上,spring boot帮我们管理了各个依赖的版本,使各个依赖不会出现版本冲突;另外,帮我们打包了各个依赖让我们不用再像之前那样自己导入一大堆的依赖,只要引入起步依赖的坐标就可以进行web开发了,同样体现了依赖传递的用。
自动装配:
自动装配也是spring boot的一大特性,新建spring boot项目之后,会发现在启动类上有个@SpringBootApplication注解,而springboot也是通过该注解去自动装配的,点进去@SpringBootApplication注解可看到如下内容:

image
可以看到,@SpringBootApplication注解可以算是由三个注解组成的,@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan。由这三个注解组成,@ComponentScan就很熟悉啦,就是用来配置spring容器扫描路径的,默认指扫描当前启动类所在的包里的对象,如果你想在本服务使用其他服务的类,可以在@SpringBootApplication注解后面添加(scanBasePackages = {"其他服务包路径:如com.aaa.bbb", "本服务包路径"}),接下来看下@SpringBootConfiguration注解:
这里面啥都没有,其实@SpringBootConfiguration注解就等同于@Configuration注解,而@Configuration注解也很熟悉啦,使用注解@Configuration的类,就表示这个类是SpringBoot bean的创建的配置文件类。启动类标注了@SpringBootConfiguration之后,本身其实也是一个ioc容器的配置类。

最终要的是@EnableAutoConfiguration这个注解,这个注解是SpringBoot自动配置功能开启,是自动装配的核心注解,点进去看下:
可以看到,@EnableAutoConfiguration这个注解使用了@AutoConfigurationPackage注解,还导入了一个AutoConfigurationImportSelector的类,先来看下@AutoConfigurationPackage注解,这个注解其实就是导入了一个AutoConfigurationPackages.Registrar.class的组件,如下图:


可以看到这个类有两个方法,主要看第一个方法:registerBeanDefinitions,顾名思义,这个方法就是用来注册bean,register方法会扫描主配置类所在包及其子包下的组件,并注册到IOC容器中。这就是@AutoConfigurationPackage注解的作用。

接下来看下导入的AutoConfigurationImportSelector类是做什么的,我们只用关注getCandidateConfigurations()这个方法:
在方法第一行打个断点看一下:

可以看到,configurations里面,就是获取的全类名集合,那么这些配置类是在哪里扫描到的呢?看下下面的报错提示信息就知道了,可以看到是在META-INF/spring.factories文件内获取的,而这个文件,就在当前类所在的包:



image
看到这里,就比较清晰了,自动装配就是扫描到这些配置类,然后进行装配的。

来看下下面这个类是怎么装配的:
下面标记出来的这个注解就是装配配置文件:

点进去ServerProperties类看一下:

可以看到,这个类读取配置文件以server开头,比如server.port、server.address等,这些配置文件是在当前包下的spring-configuration-metadata.json里面:

点进去可以看到很多json数据:

搜索一下server.port、server.address:


通过这些json数据进行相应装配,比如server.port,其实就是tomcat默认的端口号,有个defaultValue,默认值就是8080。

总结:
自动配置,第一得益于能扫描主配置类所在包及其子包的组件,将这些组件注册到IOC容器中,第二它能借助spring框架原有的SpringFactoriesLoader的支持,加载META-INF/spring.factories获取组件的全类名并通过反射实例化为对应的标注了@Configuration的JavaConfig形式,并且符合要求@Conditional要求的IOC容器配置类,然后通过一些默认值来进行相应初始化配置。
前面说了每个Spring Boot项目都要依赖于spring-boot-starter-parent,看下spring-boot-starter-parent的pom文件:

image
可以看到,这里就是加载配置文件的地方,配置文件可以以application开头,yml、yaml、properties结尾,加载顺序为yml -> yaml -> properties,后面的配置会覆盖前面的配置,比如本项目有个yml配置文件,还有个properties配置文件,两个配置文件内都有server.port,yml里面的值为8081,properties里面的值为8082,那么启动项目之后tomcat的端口就是8082,会覆盖掉yml里面的配置。

网友评论