SpringBoot原理解析

springboot启动类

@SpringBootApplication
public class HelloworldApplication {

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

}

流程图:
在这里插入图片描述

注解分析

@SpringBootApplication

springboot的核心配置,目的是开启自动配置

@ComponentScan

自动扫描符合条件的组件(比如:@Conteoller, @Service, @ Repository, @Compoent注释的类)加入到IOC容器中

我们可以通过basePackages来指定@ComponentScan扫描的范围,如果不指定,默认扫描@ComponentScan所在类的包进行扫描,所以springboot的启动类放在根包下,可以不用指定包扫描范围

@SpringBootConfiguration

和@Configuration注解类似,声明当前类是配置类

如果当前类中含有@Bean注解标记的方法,会将该方法注入IOC容器中

@EnableAutoConfiguration

@EnableAutoConfiguration是springboot实现自动化配置的核心注解,通过这个注解把spring应用所需要的bean注入容器中

@AutoConfigurationPackage

@AutoConfigurationPackage注解的作用是将该注解的类所在的package作为自动配置package进行管理

@Import(AutoConfigurationImportSelector.class)

AutoConfigurationImportSelector:自动导入配置的选择器,根据我们的配置,动态加载所需要的bean

selectImports()

@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
   if (!isEnabled(annotationMetadata)) {
      return NO_IMPORTS;
   }
   AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata); // 获取自动配置实体
   return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}

getAutoConfigurationEntry()

protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
   if (!isEnabled(annotationMetadata)) {
      return EMPTY_ENTRY;
   }
   AnnotationAttributes attributes = getAttributes(annotationMetadata);
   List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); // 获取候选配置
   configurations = removeDuplicates(configurations); // 删除重复的配置
   Set<String> exclusions = getExclusions(annotationMetadata, attributes);
   checkExcludedClasses(configurations, exclusions);
   configurations.removeAll(exclusions);
   configurations = getConfigurationClassFilter().filter(configurations);
   fireAutoConfigurationImportEvents(configurations, exclusions);
   return new AutoConfigurationEntry(configurations, exclusions);
}

getCandidateConfigurations()

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
   List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
         getBeanClassLoader()); // SpringFactoriesLoader才是自动配置的关键,其功能就是从指定文件META-INF/spring.factories加载配置
   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;
}

SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());

SpringFactoriesLoader才是自动配置的关键,其功能就是从指定文件META-INF/spring.factories加载配置

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
		String factoryTypeName = factoryType.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
	}

	private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
		MultiValueMap<String, String> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		try {
			Enumeration<URL> urls = (classLoader != null ? // 判断classLoader是否为空
					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : // null, 加载项目资源配置
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); // !null, 加载系统资源配置
			result = new LinkedMulClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION))tiValueMap<>();
			while (urls.hasMoreElements()) { // 遍历所有配置
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource); // 加载加载到Properties中
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					String factoryTypeName = ((String) entry.getKey()).trim();
					for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
						result.add(factoryTypeName, factoryImplementationName.trim());
					}
				}
			}
			cache.put(classLoader, result);
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

loadSpringFactories() 方法中会判断classloader是否为null?

如果为null,调用classLoader.getResources(FACTORIES_RESOURCE_LOCATION)加载项目资源配置,

如果不为null,调用ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)加载系统资源配置

但其实都是 FACTORIES_RESOURCE_LOCATION = “META-INF/spring.factories”

所以都会去自动加载 spring-boot-autoconfigure-jar 中的 META-INF/spring.factories 文件

@ConditionalOnXXX

META-INF/spring.factories中的配置类不是全部生效的,需要导入对应的starter才能生效,@ConditionalOnXXX()中的所有条件满足,才能生效

其他

@ConfigurationProperties

将配置文件中配置的每一个属性的值,映射到这个组件中

prefix属性:指定具体配置的前缀

静态资源存储位置

源码位置:org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {  // 判断是否配置有自动配置,如果配置了,则不使用默认配置
      logger.debug("Default resource handling disabled");
      return;
   }
   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
   if (!registry.hasMappingForPattern("/webjars/**")) { // 1.webjars目录
      customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
   String staticPathPattern = this.mvcProperties.getStaticPathPattern();
   if (!registry.hasMappingForPattern(staticPathPattern)) { // 2. /**目录
      customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
            .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
}

1、/webjars/**

在这里插入图片描述

webjars目录位置:导入包的/META-INF/resources/webjars/**

2、/**

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
      "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

查看源码发现/**目录下可以使用以下四个目录存放静态资源文件

​ “classpath:/META-INF/resources/”,“classpath:/resources/”, “classpath:/static/”, “classpath:/public/”

优先级:/META-INF/resources/ > /resources/ > /static/ > /public/

首页

源码位置:org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
      FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
   WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
         new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
         this.mvcProperties.getStaticPathPattern());
   welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
   welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
   return welcomePageHandlerMapping;
}

private Optional<Resource> getWelcomePage() {
   String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
   return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}

private Resource getIndexHtml(String location) {
   return this.resourceLoader.getResource(location + "index.html");
}

查看源码发现首页指定存放位置为:this.resourceProperties.getStaticLocations(),就是静态资源的/**目录,首页名称为index.html,如果在该目录下配置了主页,就可以直接访问到


版权声明:本文为Lzp_csdn1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Lzp_csdn1/article/details/109237329