什么是SpringBoot?
回答這個問題之前,我先回答一下什么是Spring?
Spring框架是一個開放源代碼的應用框架,是針對Bean的生命周期進行管理的輕量級容器。
Spring是為了解決企業級應用開發的復雜性而創建的,主要的目的是“簡化開發“。
Spring如何簡化開發?
基于POJO的輕量級和最小入侵式編程。
通過IOC/DI和面向接口實現松耦合。
基于切面AOP策略和模板方法進行聲明式編程。
通過切面和模板減少樣式代碼。
SpringBoot的出現就是進一步完善Spring的目的”簡化開發“,提出了約定優于配置的思想,通過“自動裝配”,實現了進一步的"簡化開發"。
SpringBoot如何實現自動裝配?
Spring實現配置的方式??梢苑譃槿N:JavaConfig、XML、注解。
SpringBoot實現自動裝配的方式是注解+JavaConfig。
SpringBoot啟動類注解 @SpringBootApplication :
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration // springboot的核心配置
@EnableAutoConfiguration // 自動裝配默認啟用
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
//... 部分代碼可自行查看源碼
}
@EnableAutoConfiguration 注解內部的代碼如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage // 自動裝配包
@Import({AutoConfigurationImportSelector.class}) // 導入 自動裝配選擇器類 由此深入了解
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
AutoConfigurationImportSelector.class 部分代碼:
/**
AutoConfigurationImportSelector.class 部分代碼
簡單剖析
*/
// 獲得所有候選配置類的路徑
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
// 找到META-INF/spring.factories中的配置類的路徑
List<String> configurations = SpringFactoriesLoader
.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
// 斷言一下這個list是否為空,如果為空就報錯
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();
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoader == null) {
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
}
String factoryTypeName = factoryType.getName();
// 核心是這個方法
return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}
loadSpringFactories();
// 這個方法就是把SpringBoot所有存在的自動裝配的配置類的路徑返回
// 同時存儲到SpringFactoriesLoader.class 的 static final Map<ClassLoader, Map<String, List<String>>> cache = new ConcurrentReferenceHashMap(); 容器中
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
Map<String, List<String>> result = (Map)cache.get(classLoader);
if (result != null) {
return result;
} else {
HashMap result = new HashMap();
try {
// SpringBoot把所有的自動裝配的配置類都存放在META-INF/spring.factories
Enumeration urls = classLoader.getResources("META-INF/spring.factories");
while(urls.hasMoreElements()) {
URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Iterator var6 = properties.entrySet().iterator();
while(var6.hasNext()) {
Entry<?, ?> entry = (Entry)var6.next();
String factoryTypeName = ((String)entry.getKey()).trim();
String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
String[] var10 = factoryImplementationNames;
int var11 = factoryImplementationNames.length;
for(int var12 = 0; var12 < var11; ++var12) {
String factoryImplementationName = var10[var12];
((List)result.computeIfAbsent(factoryTypeName, (key) -> {
return new ArrayList();
})).add(factoryImplementationName.trim());
}
}
}
result.replaceAll((factoryType, implementations) -> {
return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
});
// 加入容器中
cache.put(classLoader, result);
return result;
} catch (IOException var14) {
throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
}
}
}
spring..factories 概覽
# AutoConfigureCache auto-configuration imports
org.springframework.boot.test.autoconfigure.core.AutoConfigureCache=\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
# AutoConfigureDataCassandra auto-configuration imports
org.springframework.boot.test.autoconfigure.data.cassandra.AutoConfigureDataCassandra=\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration
結論:
Springboot所有自動裝配的類都是在啟動的時候掃描進行加載,通過加載META-INF/spring.factories文件得到所有類的路徑,然后通過@condition條件判斷注解判斷存在該jar包再進行自動加載。
具體的層級和實現具體看下圖:
@EnableAutoConfiguration.jpg