浅谈springboot原理(源码分析)

自动装配原理

pom.xml

  • spring-boot-dependencies:核心依赖在父工程中!
    资源配置(已经配置好)
<resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
  • 我们在写或引入spring依赖的时候 不需要指定版本,因为有版本仓库

启动器

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 启动器:springboot的启动场景
  • 例如:spring-boot-starter-web,会帮我们导入所有的web环境依赖
  • springboot 会将所有功能场景变成启动器
  • 我们要使用什么功能只需要找到对应的启动器

主程序

package com.example2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo2Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo2Application.class, args);
    }
}

此处是按目录顺序列出

  • @SpringBootApplication:标注这个类是一个springboot的应用:启动类下的所有资源
  • SpringApplication.run(Demo2Application.class, args);:将springboot启动
  • SpringBootApplication里面的其它注解
    • @SpringBootConfiguration:springboot的配置
      • @Configuration:spring配置类
        • @Component:spring的注解
  • @EnableAutoConfiguration:自动配置
    • @AutoConfigurationPackage:自动配置包
      • @Import(AutoConfigurationPackages.Registrar.class):自动配置·包注册 ·
    • @Import({AutoConfigurationImportSelector.class}):自动配置导入选择
      获取所有配置
      List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
      获取候选的配置
	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;
	}

META-INF/spring.factories:自动配置的核心文件
在这里插入图片描述
在这里插入图片描述
结论:

  • SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值
  • 将这些值作为自动配置类导入容器 , 自动配置类就生效 , 帮我们进行自动配置工作;
  • 整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中;
  • 它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;
  • 有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;
  • 在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊~小 l i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值