Python 官方文档:入门教程 => 点击学习
目录运行原理 pom.xml 主程序 导入的选择器分析:spring.factories文件总结 主启动类 运行原理 为了研究,我们正常从父项目的pom.xml开始进行研究。 po
为了研究,我们正常从父项目的pom.xml开始进行研究。
父依赖 spring-boot-starter-parent主要用来管理项目的资源过滤和插件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
点父依赖进去查看,发现还有一个父依赖spring-boot-dependencies,这里的这个父依赖才是真正管理SpringBoot应用里面的所有依赖版本的地方,是springboot的版本控制中心。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
启动器:spring-boot-starter-xxx:springboot的场景启动器
spring-boot-starter-WEB:导入web依赖的组件
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@SpringBootApplication
作用:标注这是一个springboot主程序类,说明这是一个springboot应用,springboot就是运行这个类的mian方法启动的springboot应用。
@SpringBootApplication //标注这是一个主程序类,说明这是一个springboot应用
public class Springboot01HelloworldApplication {
public static void main(String[] args) {
//这里启动了一个服务,而不是执行了一个方法。
SpringApplication.run(Springboot01HelloworldApplication.class, args);
}
}
点@SpringBootApplication继续研究,会发现有@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan这三个注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
1.@ComponentScan: spring自动扫描包
这个我们在spring配置文件中见到过,它用来自动扫描并加载符合条件的组件或者bean,并将bean加载到ioc容器中。
2.@SpringBootConfiguration: springboot的配置类
标注在某个类上,说明这个类是springboot的配置类,在这里它就说明SpringBootApplication这个类是springboot的配置类。
我们继续点@SpringBootConfiguration进去查看,会发现 @Configuration这个注解
2.1 @Configuration:配置类,用来配置spring的xml文件
我们继续点@Configuration进去查看,会发现 @Component这个注解。
2.2 @Component:组件,说明启动类本身也是一个组件,负责启动应用。
至此,@SpringBootConfiguration这条线,我们研究完了。
3.@EnableAutoConfiguration:开启自动装配,通过@EnableAutoConfiguration来帮我们自动配置之前我们需要配置的东西。
我们继续点@EnableAutoConfiguration进去查看,会发现 @AutoConfigurationPackage和@Import({AutoConfigurationImportSelector.class}) 这两个注解。
@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 {};
}
3.1 @AutoConfigurationPackage自动装配包
继续点进去查看,出现@Import({ReGIStrar.class})这个注解
3.1.1 @Import({Registrar.class}): spring底层注解,给容器导入一个组件
Registrar.class: 将主启动类所在包及所在包下面的所有子包里面所有的组件都扫描到Spring容器。
至此,@AutoConfigurationPackage这条线我们也研究完了。
3.2 @Import({AutoConfigurationImportSelector.class}): 给容器导入组件
AutoConfigurationImportSelector.class:自动装配导入选择器。
1.我们点进去AutoConfigurationImportSelector.class这个类的源码进行探究,
2.我们点击getCandidateConfigurations进一步分析
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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;
}
2.1 使用了getSpringFactoriesLoaderFactoryClass()方法,返回一开始我们看到的启动自动配置文件的注解类EnableAutoConfiguration.class
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
2.2 发现它调用了SpringFactoriesLoader类的静态方法,我们点击loadFactoryNames进入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()方法,点进去查看
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 {
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);
}
}
}
源码分析:
WebmvcAutoConfiguration
我们在上面的自动配置类随便找一个打开看看,比如 :WebMvcAutoConfiguration
都是大家熟悉的配置,所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件 ,并将其中对应的 org.springframework.boot.autoconfigure. 包下的配置项,通过反射实例化为对应标注了 @Configuration的JavaConfig形式的IOC容器配置类 , 然后将这些都汇总成为一个实例并加载到IOC容器中。
SpringApplication
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args);
}
}
分析:
SpringApplication主要做的事情:
以上就是springboot自动装配原理初识的详细内容,更多关于springboot自动装配原理的资料请关注编程网其它相关文章!
--结束END--
本文标题: springboot自动装配原理初识
本文链接: https://www.lsjlt.com/news/122897.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0