走進Spring Boot源碼學習之路和淺談入門

Spring Boot淺聊入門

**本人部落格網站 **IT小神 www.itxiaoshen.com

Spring Boot官網地址//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

image-20210811090719315

目前Spring boot和Spring Framework項目都是使用Gradle自動化構建工具,編譯直接查看項目根目錄下README.adoc文件即可,有時網路不好可以多次編譯重試

image-20210811090507786

零配置

Spring Web MVC簡述

Spring Web Mvc官網地址 //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基於內嵌容器特性會漸漸淡化父子容器的概念,最終只是一個大容器

image-20210810174536660

內嵌容器

概述

Spring Boot 內嵌像Tomcat、Jetty、Undertow等實現Servlet規範的容器,如果有研究過Tomcat這隻三腳貓logo中間件(後續有時間我們再分享一下Tomcat源碼和原理)也知道Tomcat也是一個用Java語言編寫的強大中間件,在Spring Boot通過New Tomcat方式來配置和啟動web容器。

Spring Framework源碼編譯

使用最新Release版本v5.2.16.RELEASE

image-20210811091403678

同樣找到項目根目錄下README.md,從githubSpring Framework源碼 wiki找到編譯源碼說明

image-20210811091723913

image-20210811091927447

實現

根據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全類名

image-20210810175831748

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容器的

image-20210811094042778

image-20210811094642698

找到其實現類ServletWebServerApplicationContext,在這裡createWebServer開始創建WebServer,

image-20210811094919733

image-20210811100427533

這裡會根據我們我們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的源碼

image-20210811102649821

image-20210811102803486

	@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項目里實現的

image-20210811104306064

而我們經常看到spring-boot-starter-xxx這種裡面主要是定義了依賴包裝成一個個啟動器,實際實現還是在spring-boot-autoconfigure中,這些都是Spring Boot官方幫我們整合實現並提供開箱即可工具,此外還有一些第三方整合提供以xxx開頭的xxx-spring-boot-starter比如阿里巴巴的druid-spring-boot-starter和苞米豆mybatis-plus-boot-starter等

image-20210811104925144

在@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 的時候再來詳說

image-20210811110931317

image-20210811111307436

image-20210811113250346

重要的方法也即是這個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;
	}

image-20210811111853490

通過spring-boot-autoconfigure的resource目錄下META-INF目錄找到spring.factories文件後我們可以看到這裡面就是Spring SPI機制實現,在此定義一個規範的路徑和文件名,文件內容放置了一個個Spring Boot提供自動裝配的組件,而後面我們可想而知Spring Boot底層肯定還是利用反射機制將這些類放到Spring容器中管理,我們就可以通過Spring Boot通用三板斧使用步驟,第一步加坐標依賴starter,第二步啟動類中開啟使用註解,第三步配置配置文件參數後輕鬆使用工具類的功能

image-20210811112441095

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")
}