Spring Boot源码解析:Spring 和 Spring Boot 有哪些区别?

在这里插入图片描述

手写一个Spring Boot

当别人问你Spring和Spring Boot的区别时,你可能会随口而出

  1. 应用可以直接运行,而不用打成jar包再放到tomcat中运行
  2. 当使用某些功能时,引入相应的starter即可,不用再配置各种Bean了

其实我觉得自动装配才是Spring Boot最大的创新。应用直接运行我们用嵌入式tomcat就可以办到。我们把之前的spring mvc项目改为直接运行

依赖如下

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.19.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.2.19.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-core</artifactId>
  <version>8.0.28</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-logging-juli</artifactId>
  <version>8.0.28</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>8.0.28</version>
</dependency>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这3个类就是我们之前分析Spring MVC源码用到的,工作原理我就不再重复了。现在我们要把它改造为直接通过main方法运行
在这里插入图片描述
在这里插入图片描述
点击main方法即可运行,和我们启动Spring Boot的方式很类似把。这里说个挺有意思的问题,为什么我们这个Demo运行完main方法后为什么没有直接退出呢?

其实导致进程退出的条件之一是非daemon线程完全终止,那么我们只需要应用中包含一个非daemo线程不会终止,main在运行完就不会退出。上面的awaitThread就是一个非daemon线程,并且一直在阻塞监听请求,所以在main方法运行完毕后,不会退出,spring boot也是用这种方式实现的

一个典型的Spring Boot的启动类如下

@SpringBootApplication
public class BlogApplication {
	public static void main(String[] args) {
		SpringApplication.run(BlogApplication.class, args);
	}
}

写成如下形式你可能更好理解

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class BlogConfiguration {
}


public class BlogApplication {
	public static void main(String[] args) {
		SpringApplication.run(BlogConfiguration.class, args);
	}
}

可以看到并没有多少新奇的东西,@Configuration和@ComponentScan我们在之前就接触到了,而@EnableAutoConfiguration算是一个创新,用来实现自动装配。下节我们会分析@EnableAutoConfiguration是如何实现自动装配的。

出去自动装配,Spring Boot的源码和我们代码的区别就是

Spring容器是在main中启动spring容器和tomcat的,在WebApplicationInitializer实现类中,将DispatcherServlet注册到tomcat中

而我们的代码只在main方法中启动了tomcat容器,在WebApplicationInitializer实现类中启动spring容器和将DispatcherServlet注册到tomcat中。你可以将上面的代码改一下,改成和Spring容器一样的形式

参考博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java识堂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值