• SpringBoot自动装配


    SpringBoot自动装配

    启动器

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starterartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    请添加图片描述

    • 启动器就是SpringBoot的启动场景
    • 比如spring-boot-starter-web,SpringBoot就会自动导入web环境所有的依赖。
    • SpringBoot会将所有的功能场景都变成一个个的启动器。

    主程序

    //@SpringBootApplication 标准这个类是一个SpringBoot应用
    @SpringBootApplication
    @MapperScan("com.wk.poem.mapper")
    public class DemoApplication {
    
        public static void main(String[] args) {
            //将SpringBoot应用启动
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    注解
    @SpringBootConfiguration    		//SpringBoot的配置
    	@Configuration					//spring配置类
    		@Component					//说明这也是一个spring的组件
    @EnableAutoConfiguration			//自动配置
    	@AutoConfigurationPackage		//自动配置包
    		@Import({Registrar.class})	//自动配置包注册
    	@Import({AutoConfigurationImportSelector.class})	//自动配置导入选择
    		//获取所有的配置
    		List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
        ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    请添加图片描述

    具体流程图:https://www.processon.com/view/link/62ff92ae1efad47d12435e63

    密码:3l9P

    结论:SpringBoot所有的自动配置都是在启动的时候扫描加载:spring.factories所有的自动配置类都在这里。

  • 相关阅读:
    InstallAnywhere制作安装包
    大数据平台测试-高级架构师语录(偷笑)
    Arduino框架下ESP32使用固件自带的SD库的总结
    逆向-beginners之栈
    2023-2028年中国六氟化钨市场发展态势及投资建议报告
    selenium键盘鼠标事件ActionChains
    TDengine 3.0 重磅发布,首届开发者大会圆满结束
    FTP服务器:ExpanDrive Mac
    python系列教程191——nonlocal边界
    快速排序(java语言实现)
  • 原文地址:https://blog.csdn.net/weixin_43416686/article/details/126443242