走进Spring Boot源码学习之路和浅谈入门
yuyutoo 2024-10-26 16:09 12 浏览 0 评论
Spring Boot浅聊入门
Spring Boot官网地址:https://spring.io/projects/spring-boot/
Spring Boot可以轻松创建独立的、基于Spring的产品级应用程序“直接运行”。
作为笔者见解,Spring Boot 不算是一个全新的框架,Spring Boot 底层还是大量依赖于Spring Framework,而Spring Framework很早以前版本就已提供基于注解、Java Config而不仅是XML配置编程;Spring Boot采用约定大于配置方式替代xml配置,是Spring Framework一个大升级版本,整合很多自动装配组件,让开发者开箱即用,接下来我们一起来聊聊Spring Boot三个重要的特性和starter开发简易剖析
Spring Boot源码编译
使用最新Releases版本v2.5.3
目前Spring boot和Spring Framework项目都是使用Gradle自动化构建工具,编译直接查看项目根目录下README.adoc文件即可,有时网络不好可以多次编译重试
零配置
Spring Web MVC简述
Spring Web Mvc官网地址** **https://docs.spring.io/spring-framework/docs/current/reference/html/web.html
Spring Web MVC是基于Servlet API的原始Web框架,从一开始就包含在Spring框架中。“Spring Web MVC”的正式名称来自其源模块的名称(Spring -webmvc),但它通常被称为“Spring MVC”
零配置实现
Spring Web MVC官网开篇最前面第一章第一小节也即是1.1 DispatcherServlet中就重点引用一下这六句代码实现原来我们Spring MVC 工程web.xml里面配置功能,仅通过下面这一小段代码就初始化了Spring根容器和Web子容器,这种方式也是官方推荐使用的
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
?
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}
而我们非常熟悉的Spring Web Mvc工程web.xml配置示例注册和初始化DispatcherServlet:
<web-app>
?
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param>
?
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
?
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
?
</web-app>
父子容器
个人猜测Spring Boot基于内嵌容器特性会渐渐淡化父子容器的概念,最终只是一个大容器
内嵌容器
概述
Spring Boot 内嵌像Tomcat、Jetty、Undertow等实现Servlet规范的容器,如果有研究过Tomcat这只三脚猫logo中间件(后续有时间我们再分享一下Tomcat源码和原理)也知道Tomcat也是一个用Java语言编写的强大中间件,在Spring Boot通过New Tomcat方式来配置和启动web容器。
Spring Framework源码编译
使用最新Release版本v5.2.16.RELEASE
同样找到项目根目录下README.md,从githubSpring Framework源码** wiki找到编译源码说明**
实现
根据Servlet从3.0版本之后的规范,所有实现Servlet规范的Web容器都会扫描jar包下面META-INF/services下的文件,在Spring Web子项目中利用Java 的SPI机制(SPI是一种扩展机制,很多框架底层都使用或甚至重新实现自己的SPI机制,比如像Spring和Dubbo都实现自己的SPI,以后我们再找时间来讨论,暂且到这里),在根目录下META-INF/services创建一个文件名javax.servlet.ServletContainerInitializer,文件内容是实现类的org.springframework.web.SpringServletContainerInitializer全类名
SpringServletContainerInitializer实现类针对通过@HandlesTypes感兴趣注解, 将所有实现WebApplicationInitializer的实现类放到一个Set集合中,然后再逐个调用其onStartup()方法,所以这里我们接上了1.1章节官网MyWebApplicationInitializer实现WebApplicationInitializer接口这一部分
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
?
List<WebApplicationInitializer> initializers = Collections.emptyList();
?
if (webAppInitializerClasses != null) {
initializers = new ArrayList<>(webAppInitializerClasses.size());
for (Class<?> waiClass : webAppInitializerClasses) {
// Be defensive: Some servlet containers provide us with invalid classes,
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer)
ReflectionUtils.accessibleConstructor(waiClass).newInstance());
}
catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
?
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
?
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
?
}
接下来我们再来看看Web容器如Tomcat是如何在Spring Boot中运行的,首先我们先得知道SpringBoot启动过程,我们知道Spring Boot 应用程序都是从SpringApplication.run开始的,而run方法则始于SpringApplication类,返回ApplicationContext接口的子接口ConfigurableApplicationContext,通过refreshContext方法调用Spring Framework最大名鼎鼎的refresh方法初始化Spring容器,而refresh方法中的onRefresh是留给子类扩展使用的,而也是在这里开始初始化Web容器的
找到其实现类ServletWebServerApplicationContext,在这里createWebServer开始创建WebServer,
这里会根据我们我们pom文件starter依赖来决定创建Tomcat、Undertow、Jetty,Undertow在并发下性能明显优化另外Tomcat和Jetty,建议优先使用Undertow,但这里由于大家对于Tomcat印象比较深刻,所以我们示例还是以Tomcat为主
Spring Boot内嵌容器默认为Tomcat,想要换成Undertow也是非常容易的,只需修改spring-boot-starter-web依赖,移除tomcat的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
添加undertow依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
server:
port: 8084
http2:
enabled: true
undertow:
io-threads: 16
worker-threads: 256
buffer-size: 1024
buffers-per-region: 1024
direct-buffers: true
至此,这里我们通过ServletWebServerFactory的实现类TomcatServletWebServerFactory的getWebServer方法找到new Tomcat的源码
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
if (this.disableMBeanRegistry) {
Registry.disableRegistry();
}
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
connector.setThrowOnFailure(true);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatWebServer(tomcat);
}
自动装配
@SpringBootApplication注解
我们知道所有运用Spring Boot应用程序在根目录的主类上都标有一个@SpringBootApplication的注解,这个就是SpringBoot自动装配实现的入口,我们接下来一步步的来分析,首先@SpringBootApplication在org.springframework.boot.autoconfigure.SpringBootApplication中,Spring Boot的自动装配也都是在org.springframework.boot.autoconfigure项目里实现的
而我们经常看到spring-boot-starter-xxx这种里面主要是定义了依赖包装成一个个启动器,实际实现还是在spring-boot-autoconfigure中,这些都是Spring Boot官方帮我们整合实现并提供开箱即可工具,此外还有一些第三方整合提供以xxx开头的xxx-spring-boot-starter比如阿里巴巴的druid-spring-boot-starter和苞米豆mybatis-plus-boot-starter等
在@SpringBootApplication注解里主要有下面这三个注解
- @SpringBootConfiguration(继承了Configuration,表示当前是注解类)
- @ComponentScan (包含扫描和排除扫描组件配置)
- @EnableAutoConfiguration (在这里开启Spring Boot的自动配置注解功能)@AutoConfigurationPackage**@Import(AutoConfigurationImportSelector.class) **
核心之核心的地方就是借助@import导入AutoConfigurationImportSelector,这个是Spring中ImportSelector接口的实现类,ImportSelect接口是org.springframework.context里的内容,在Spring Framework 3.1版本之后就有,后续我们有时间讨论Spring Framework 的时候再来详说
重要的方法也即是这个getCandidateConfigurations,调用loadFactoryNames,而loadFactoryNames调用loadSpringFactories方法,classLoader.getResources这里就是谜底揭开的时候,通过JDK classLoader加载FACTORIES_RESOURCE_LOCATION(META-INF/spring.factories)文件内容
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
?
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
}
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}
?
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
Map<String, List<String>> result = cache.get(classLoader);
if (result != null) {
return result;
}
?
result = new HashMap<>();
try {
Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
String[] factoryImplementationNames =
StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
for (String factoryImplementationName : factoryImplementationNames) {
result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
.add(factoryImplementationName.trim());
}
}
}
?
// Replace all lists with unmodifiable lists containing unique elements
result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
cache.put(classLoader, result);
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
return result;
}
通过spring-boot-autoconfigure的resource目录下META-INF目录找到spring.factories文件后我们可以看到这里面就是Spring SPI机制实现,在此定义一个规范的路径和文件名,文件内容放置了一个个Spring Boot提供自动装配的组件,而后面我们可想而知Spring Boot底层肯定还是利用反射机制将这些类放到Spring容器中管理,我们就可以通过Spring Boot通用三板斧使用步骤,第一步加坐标依赖starter,第二步启动类中开启使用注解,第三步配置配置文件参数后轻松使用工具类的功能
Starter开发简易剖析
步骤
- 建立autoconfigure工程开发自动配置的实现类xxxProperties.class 提供参数配置实现类,比如redis的host、url等,我们也可以从这里找到所有配置参数信息xxxConnectionConfiguration.class,由@Import注解导入依赖类xxxAutoConfiguration实现类,然后通过Java Config和@Bean注解方式将Java Bean注册到Spring容器里,后续使用则直接从Spring容器中拿即可项目根目录resource建立META-INF文件夹,下面建立spring.factories文件,将自动配置类的全类名方放在org.springframework.boot.autoconfigure.EnableAutoConfiguration里
- 建立spring-boot-starter-xxx工程,管理依赖,如下Redis Starter gradle配置
RedisAutoConfiguration分析
RedisAutoConfiguration实现
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
?
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
?
@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
?
}
Redis Starter依赖
plugins {
id "org.springframework.boot.starter"
}
?
description = "Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client"
?
dependencies {
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
api("org.springframework.data:spring-data-redis")
api("io.lettuce:lettuce-core")
}
相关推荐
- 当 Linux 根分区 (/) 已满时如何释放空间?
-
根分区(/)是Linux文件系统的核心,包含操作系统核心文件、配置文件、日志文件、缓存和用户数据等。当根分区满载时,系统可能出现无法写入新文件、应用程序崩溃甚至无法启动的情况。常见原因包括:...
- 玩转 Linux 之:磁盘分区、挂载知多少?
-
今天来聊聊linux下磁盘分区、挂载的问题,篇幅所限,不会聊的太底层,纯当科普!!1、Linux分区简介1.1主分区vs扩展分区硬盘分区表中最多能存储四个分区,但我们实际使用时一般只分为两...
- Linux 文件搜索神器 find 实战详解,建议收藏
-
在Linux系统使用中,作为一个管理员,我希望能查找系统中所有的大小超过200M文件,查看近7天系统中哪些文件被修改过,找出所有子目录中的可执行文件,这些任务需求...
- Linux 操作系统磁盘操作(linux 磁盘命令)
-
一、文档介绍本文档描述Linux操作系统下多种场景下的磁盘操作情况。二、名词解释...
- Win10新版19603推送:一键清理磁盘空间、首次集成Linux文件管理器
-
继上周四的Build19592后,微软今晨面向快速通道的Insider会员推送Windows10新预览版,操作系统版本号Build19603。除了一些常规修复,本次更新还带了不少新功能,一起来了...
- Android 16允许Linux终端使用手机全部存储空间
-
IT之家4月20日消息,谷歌Pixel手机正朝着成为强大便携式计算设备的目标迈进。2025年3月的更新中,Linux终端应用的推出为这一转变奠定了重要基础。该应用允许兼容的安卓设备...
- Linux 系统管理大容量磁盘(2TB+)操作指南
-
对于容量超过2TB的磁盘,传统MBR分区表的32位寻址机制存在限制(最大支持2.2TB)。需采用GPT(GUIDPartitionTable)分区方案,其支持64位寻址,理论上限为9.4ZB(9....
- Linux 服务器上查看磁盘类型的方法
-
方法1:使用lsblk命令lsblk输出说明:TYPE列显示设备类型,如disk(物理磁盘)、part(分区)、rom(只读存储)等。...
- ESXI7虚机上的Ubuntu Linux 22.04 LVM空间扩容操作记录
-
本人在实际的使用中经常遇到Vmware上安装的Linux虚机的LVM扩容情况,最终实现lv的扩容,大多数情况因为虚机都是有备用或者可停机的情况,一般情况下通过添加一块物理盘再加入vg,然后扩容lv来实...
- 5.4K Star很容易!Windows读取Linux磁盘格式工具
-
[开源日记],分享10k+Star的优质开源项目...
- Linux 文件系统监控:用脚本自动化磁盘空间管理
-
在Linux系统中,文件系统监控是一项非常重要的任务,它可以帮助我们及时发现磁盘空间不足的问题,避免因磁盘满而导致的系统服务不可用。通过编写脚本自动化磁盘空间管理,我们可以更加高效地处理这一问题。下面...
- Linux磁盘管理LVM实战(linux实验磁盘管理)
-
LVM(逻辑卷管理器,LogicalVolumeManager)是一种在Linux系统中用于灵活管理磁盘空间的技术,通过将物理磁盘抽象为逻辑卷,实现动态调整存储容量、跨磁盘扩展等功能。本章节...
- Linux查看文件大小:`ls`和`du`为何结果不同?一文讲透原理!
-
Linux查看文件大小:ls和du为何结果不同?一文讲透原理!在Linux运维中,查看文件大小是日常高频操作。但你是否遇到过以下困惑?...
- 使用 df 命令检查服务器磁盘满了,但用 du 命令发现实际小于磁盘容量
-
在Linux系统中,管理员或开发者经常会遇到一个令人困惑的问题:使用...
- Linux磁盘爆满紧急救援指南:5步清理释放50GB+小白也能轻松搞定
-
“服务器卡死?网站崩溃?当Linux系统弹出‘Nospaceleft’的红色警报,别慌!本文手把手教你从‘删库到跑路’进阶为‘磁盘清理大师’,5个关键步骤+30条救命命令,快速释放磁盘空间,拯救你...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)