SpringBoot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。
spring-boot-starter-web启动器自动依赖其他组件,简少了maven的配置。
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Spring Boot能根据当前类路径下的类、jar包来自动配置bean,如添加一个spring-boot-starter-web启动器就能拥有web的功能,无需其他配置。
Spring Boot提供一系列端点可以监控服务及应用,做健康检测。
Boot 的核心配置文件有哪几个?它们的区别是什么?
bootstrap (.yml 或者 .properties)
application (.yml 或者 .properties)
因此,对比 application 配置文件,bootstrap 配置文件具有以下几个特性:
boostrap 由父 ApplicationContext 加载,比 applicaton 优先加载
application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置。
使用 Spring Cloud Config 配置中心时,这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;
Boot 的配置文件有哪几种格式?它们有什么区别?
.properties 和 .yml,它们的区别主要是书写格式不同。
另外,.yml 格式不支持 @PropertySource 注解导入配置。
yml的优先级高于.properties 可以两个配置文件中都配置访问端口号,看哪个配置生效
Boot 的核心注解是哪个?它主要由哪几个注解组成的?
启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:
@SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。
@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能: @SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class })。
1. 继承spring-boot-starter-parent项目(普通spring-boot项目)
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from
repository -->
2. 导入spring-boot-dependencies项目依赖(参考时springcloud项目搭建的方法)
dependencyManagement和 dependencies区别
dependencyManagement当使用父子项目创建工程架构时,父项目可以使用该标签
来规定子项目可以使用哪些包,但是并不是直接引入,而是子项目按需引入,并且引入
dependencies使用的话,直接父项目把所有包都引入了,不管子项目是否用得到
import maven默认也是单继承 加上import 相当于父项目中使用了
多继承,既有springcloud的所有包,又有springboot的所有包,子项目,可以直接使用
Spring Boot依赖包里面的组件的版本都是和当前Spring
Boot绑定的,如果要修改里面组件的版本,只需要添加如下属性覆盖即可,但这种方式只对继承有效,导入的方式无效。
如果导入的方式要实现版本的升级,达到上面的效果,这样也可以做到,把要升级的组件依赖放到Spring Boot之前。需要注意,要修改Spring Boot的依赖组件版本可能会造成不兼容的问题。
Boot 需要独立的容器运行吗?
模拟内置tomcat功能,参考项目:embed_tomcat
org.apache.catalina.Context;
org.apache.catalina.WebResourceRoot;
org.apache.catalina.startup.Tomcat;
org.apache.catalina.webresources.DirResourceSet;
org.apache.catalina.webresources.StandardRoot;
java.io.File;
class SpringApplication {
String path =SpringApplication.class.getResource("/").getPath();
//获取webapp 文件D:\idea_qy107\embed_tomcat\src\main\webapp
String filePath = newFile("src/main/webapp").getAbsolutePath();
//然后将webapp下的项目添加至tomcat的context容器(context对应一个运行的项目)
Context context=tom.addWebapp("/my_web_app",filePath);
//webResourceRoot用于加载 项目的class文件
WebResourceRoot webResource = newStandardRoot(context);
webResource.addPreResources(newDirResourceSet(webResource,"/WEB-INF/classes",path,"/"));
,等待前端连接
在jar所在位置,shift+鼠标右键 打开命令行,输入命令:
java -jar spring_boot_interview_question.jar [启动类]
http://localhost:8082/showEntity
ctrl+c终止运行,如果是cmd命令窗口,直接关闭就可以了
Boot 自动配置原理是什么?
System.out.println("PojoClass交给了spirngioc管理了。。。 ");
2,在@Configuration注解的类上加入@Import(value = {PojoClass.class})
public Emp getById(Integer empNo){
System.out.println("springioc容器中是否存在pojoClass对象:"+pojoClass);
return empService.getById(empNo);
通过ImportBeanDefinitionRegistrar将类注入到Spring IOC容器。
public class BeanDefinitionRegisterClass {
public BeanDefinitionRegisterClass() {
System.out.println("通过ImportBeanDefinitionRegistrar接口把该类交给springioc容器进行管理。。。");
2,创建ImportBeanDefinitionRegistrar实现类
public class MyImportBeanDefinitionRegisterimplements ImportBeanDefinitionRegistrar {
//交给springioc容器管理的是BeanDefinition bean定义
RootBeanDefinition rootBeanDefinition=new RootBeanDefinition(BeanDefinitionRegisterClass.class);
registry.registerBeanDefinition("beanDefinitionRegisterClass",rootBeanDefinition);
3,在@Configuration注解的类上加入@Import(value ={MyImportBeanDefinitionRegister.class})
private BeanDefinitionRegisterClass beanDefinitionRegisterClass;
publicEmp getById(Integer empNo){
System.out.println("springioc容器中是否存在pojoClass对象:"+pojoClass);
System.out.println("springioc容器中是否存在beanDefinitionRegisterClass对象:"+beanDefinitionRegisterClass);
return empService.getById(empNo);
publicclass ImportSelectorClassA {
public ImportSelectorClassA() {
System.out.println("通过ImportSelector接口把该类ImportSelectorClassA交给springioc容器进行管理。。。");
public class MyImportSelector implements ImportSelector {
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return newString[]{"com.aaa.sbac.ac2.entity.ImportSelectorClassA",
"com.aaa.sbac.ac2.entity.ImportSelectorClassB"};
3,在@Configuration注解的类上加入@Import(value ={MyImportSelector.class})
参考项目:spring_boot_auto_configuration第3个例子:com.aaa.sbac.ac3包的例子:
Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射。
urls.nextElement();
这个方法会加载类路径及所有jar包下META-INF/spring.factories配置中映射的自动配置的类。
static final String FACTORIES_RESOURCE_LOCATION =
"META-INF/spring.factories";
Boot自带的自动配置的包:org.springframework.boot:spring-boot-autoconfigure-2.2.4.RELEASE.jar,打开其中的META-INF/spring.factories文件会找到自动配置的映射。
@Configuration,@ConditionalOnClass就是自动配置的核心,首先它得是一个配置文件,其次根据类路径下是否有这个类去自动配置。