当前位置: 首页 > news >正文

手写一个starter来理解SpringBoot的自动装配

自动装配以及简单的解析源码

自动装配是指SpringBoot在启动的时候会自动的将系统中所需要的依赖注入进Spring容器中
我们可以点开@SpringBootApplication这个注解来一探究竟
在这里插入图片描述
点开这个注解可以发现这些
在这里插入图片描述
我们点开@SpringBootConfiguration这个注解
在这里插入图片描述
可以发现实际上@SpringBootApplication这个其实是一个配置类
再点开@EnableAutoConfiguration(允许自动配置)这个注解,
在这里插入图片描述
在这里最重要的是@AutoConfigurationPackage@Import这两个注解
@AutoConfigurationPackage这个注解的作用是扫描与启动类同目录底下的所有包以及其子包,将相关的Bean注入进Spring容器中
@Import注解则是将AutoConfigurationImportSelector.class将这个类作为Bean注入进Spring容器中,我们再点开这个类来看看
在这里插入图片描述
找到这个方法,然后再点 loadFactoryNames这个方法
在这里插入图片描述
我们可以看到这段代码,这段代码的意思就是说,加载META-INF/spring.factories 这个目录底下的配置类到Spring容器里面,再根据配置类来生成相应的Bean对象
所以说这两个注解,一个是将同一个项目里面的bean注入进Spring容器中,另外一个注解是将别人写好的配置类里面的Bean注入进Spring容器中

手写一个starter帮助理解自动装配

手把手教学

首先创建一个maven工程,名字应该是xxx-spring-boot-starter(Spring官方规定,如果是第三方的starter命名规则应该是xxx-spring-boot-starter,而Spring官方的starter应该是spring-boot-xxx-starter)
创建好了以后添加如下依赖

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency></dependencies>

然后我们创建两个类
在这里插入图片描述
ConfigProperties 这个类是与Spring的配置文件关联的,配置文件中的值会被注入相应的字段中
@ConfigurationProperties(prefix = "dxg")这个注解的意思就是,在配置文件中相关配置前缀是什么

package com.DXG.config;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "dxg")
public class ConfigProperties {private Integer age;private String name;public ConfigProperties(){}public ConfigProperties(Integer age, String name) {this.age = age;this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

然后我们再编写这个配置类

@Configuration
@EnableConfigurationProperties(ConfigProperties.class)
public class ConfigPropertiesAutoConfiguration {public ConfigProperties configProperties;public ConfigPropertiesAutoConfiguration(ConfigProperties configProperties){this.configProperties = configProperties;}
}

在这个配置类中,首先打上@Configuration这个注解表明这是一个配置类
然后再打上@EnableConfigurationProperties(ConfigProperties.class)这个注解,这个注解的意思是让使用了@ConfigurationProperties(prefix = "dxg")ConfigProperties.class注入进IOC容器中
然后我们就可以根据配置来生成相应的Bean了,比如我们编写了两个Bean

    @Beanpublic TestService testService(){return new TestService(configProperties.getAge(), configProperties.getName());}@Beanpublic TestService1 testService1(){return new TestService1(configProperties.getAge());}
package com.DXG.service;public class TestService {private Integer age;private String name;public TestService(Integer age, String name) {this.age = age;this.name = name;}@Overridepublic String toString() {return "ConfigProperties{" +"age=" + age +", name='" + name + '\'' +'}';}
}
package com.DXG.service;public class TestService1 {private Integer age;public TestService1(Integer age) {this.age = age;}@Overridepublic String toString() {return "TestService1{" +"age=" + age +'}';}
}

最重要的一步来了,我们需要在META-INF这个目录下面创建spring.factories这个文件
在这里插入图片描述
在这里面输入我们的配置类,这样才能被SpringBoot扫描到然后加载进Spring容器里面生成相应的Bean

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.DXG.config.ConfigPropertiesAutoConfiguration

然后我们打包这个项目,生成相应的jar包
在这里插入图片描述
接下来我们就需要测试自动装配到底有没有生效了
在创建一个项目然后引入这个jar包

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.DXG</groupId><artifactId>test-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>

编写相应的代码来进行测试

在这里插入图片描述

package com.DXG.controller;import com.DXG.service.TestService;
import com.DXG.service.TestService1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping("/test/")
public class TestController {@Resourceprivate TestService testService;@Resourceprivate TestService1 testService1;@RequestMapping("/testString")public String testString(){return testService.toString();}@RequestMapping("/testString1")public String testString1(){return testService1.toString();}
}

在配置文件里面填写相应的配置

server:port: 8080dxg:age: 12name: "DXG"

接下来启动SpringBoot项目
在这里插入图片描述
在这里插入图片描述
可以看到,确实是将两个Bean都注入进Spring容器中供我们使用了

总结

接下来画个流程图总结一下
在这里插入图片描述

相关文章:

  • vue配置qiankun及打包上线
  • 【复现】DiffTalk
  • Java面试——框架篇
  • 【MATLAB】快速绘制曲线图的形状,粗细,颜色
  • docker run 命令详解
  • 给Flutter + FireBase 增加 badge 徽章,App启动器 通知红点。
  • base64与BytesIO图片进行编码、解码;api调用
  • rk3566 armbian修复usb2.0并挂载U盘
  • Hadoop集群环境下HDFS实践编程过滤出所有后缀名不为“.abc”的文件时运行报错:java.net.ConnectException: 拒绝连接;
  • 数据分析-Pandas如何轻松处理时间序列数据
  • 【LeetCode:228. 汇总区间 | 区间】
  • 大模型学习之书生·浦语大模型4——基于Xtuner大模型微调实战
  • 开源加解密库之GmSSL
  • jenkins忘记密码后的操作
  • 网关Gateway
  • SegmentFault for Android 3.0 发布
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • angular学习第一篇-----环境搭建
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • Python语法速览与机器学习开发环境搭建
  • Service Worker
  • Spring Boot MyBatis配置多种数据库
  • spring security oauth2 password授权模式
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • vue 配置sass、scss全局变量
  • vue的全局变量和全局拦截请求器
  • 番外篇1:在Windows环境下安装JDK
  • 一、python与pycharm的安装
  • 运行时添加log4j2的appender
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • Python 之网络式编程
  • 正则表达式-基础知识Review
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #{}和${}的区别?
  • #HarmonyOS:Web组件的使用
  • (3)选择元素——(17)练习(Exercises)
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (八)c52学习之旅-中断实验
  • (二)linux使用docker容器运行mysql
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (算法)N皇后问题
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET建议使用的大小写命名原则
  • .NET微信公众号开发-2.0创建自定义菜单
  • [ C++ ] STL---仿函数与priority_queue
  • [ NOI 2001 ] 食物链
  • [20150707]外部表与rowid.txt
  • [Angular] 笔记 16:模板驱动表单 - 选择框与选项
  • [BZOJ1178][Apio2009]CONVENTION会议中心