这篇文章适合实际操作~,主要讲述如何自定义start,实现一些自定义类的自动组装。入门的朋友,有需求想知道的,免得浪费时间~实现启动器其实就是对SpringBoot自动装配原理的一种实践。之前也写过一篇关于SpringBoot的自动状态原理的文章,文章链接认真对待,没有什么是太难的。一、SpringBoot中的SPI机制什么是spi?全称是ServiceProviderInterface。简单翻译就是服务提供者接口,就是一种寻找服务实现的机制。其实就是一个规范定义,或者说是一个实现标准。举个生活中的例子,你买了小米手机。但是,您使用的充电器不一定是小米充电器。您可以使用其他厂家的充电器进行充电。只要满足协议和端口要求,就可以进行计费。这也是热插拔的思路,不是固定的。代码也是一样的。我定义了一个接口,但是我不想固定具体的实现类,因为如果要改变实现类,就得改源码,这往往是不合适的。那我也可以定义一个规范,以后需要更换实现类或者增加其他实现类的时候,按照这个规范,我也可以动态发现这些实现类。在SpringBoot中,现在的SpringBoot平台定义了一些规范和标准,现在我想让SpringBoot平台接受我。我该怎么做?很简单,按照它的标准和规范做事。SpringBoot启动时会扫描所有jar包resource/META-INF/spring.factories文件,根据类的全限定名使用反射机制将Bean加载到容器中。看完这段话,我想你应该对今天的文章有了一个大概的了解。2.自定义starter说说我的小实践:在这个starter中,实现了发送短线的Template对象中存储的Template对象的自动组装~大致四步~类xxxxProperties用来映射配置中的配置文件用于操作xxxx的接口和client等,比如本文中的OssTemplate自动配置类xxxxAutoConfiguration,将xxxxTemplate注入到容器中,将xxxxAutoConfiguration添加到spring.factories中EnableAutoConfiguration的vaule集合中,我使用的对象存储阿里云oss,里面的配置都可以用。对于短信来说,就是模拟~别怪它。2.1准备一个Maven项目,删除src目录,然后创建两个Maven项目(我习惯创建空的Maven项目,其实创建SpringBoot项目也是如此)最外层的pom.xmlorg.springframework.bootspring-boot-starter-parent2.5.288org.projectlomboklombok工件编号>org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testorg.springframework.bootspring-boot-configuration-processortrue2.2、准备属性类就是用来映射配置文件的~/***@author宁在春*/@Data@ConfigurationProperties(prefix="nzc.oss")publicclassOssProperties{私有字符串访问密钥;私有字符串秘密;私有字符串桶名;私人字符串网址;私有字符串端点;}@Data@ConfigurationProperties(prefix="nzc.sms")publicclassSmsProperties{私有字符串名称;类就是我们最终要通过自动组装注入到SpringBoot运行的类。这里有OssTemplate和SmsTemplate/***@author宁在纯*/publicclassOssTemplate{privateOssPropertiesossProperties;publicOssTemplate(OssPropertiesossProperties){this.ossProperties=ossProperties;}publicStringtest(){System.out.println(ossProperties.getBucketName());返回“测试”;}publicStringupload(Stringfilename,InputStreamis){//yourEndpoint填写Bucket所在区域对应的Endpoint。以华东1(杭州)为例,Endpoint为https://oss-cn-hangzhou.aliyuncs.com。Stringendpoint=ossProperties.getEndpoint();//阿里云主账号AccessKey可以访问所有API,风险很大。强烈建议您创建并使用RAM账号用于API访问或日常运维。请登录https://ram.console.aliyun.com创建RAM账号。StringaccessKeyId=ossProperties.getAccessKey();StringaccessKeySecret=ossProperties.getSecret();//创建一个OSSClient实例。OSSossClient=newOSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);StringstorePath=newSimpleDateFormat("yyyy/MM/dd").format(newDate())+"/"+UUID.randomUUID()+filename.substring(filename.lastIndexOf("."));System.out.println(storePath);//依次填写Bucket名称(如examplebucket)和完整的object路径(如exampledir/exampleobject.txt)。存储桶名称不能包含在完整的对象路径中。ossClient.putObject(ossProperties.getBucketName(),storePath,is);字符串url=ossProperties.getUrl()+storePath;//关闭OSSClient.ossClient.shutdown();返回url+"#"+存储路径;}publicvoidremove(StringfileUrl){//yourEndpoint填写Bucket所在区域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。Stringendpoint=ossProperties.getEndpoint();//阿里云账号AccessKey可以访问所有API,风险很大。强烈建议您创建并使用RAM用户进行API访问或日常运维。请登录RAM控制台创建RAM用户。StringaccessKeyId=ossProperties.getAccessKey();StringaccessKeySecret=ossProperties.getSecret();//填写桶名。StringbucketName=ossProperties.getBucketName();//填写文件的完整路径。存储桶名称不能包含在完整文件路径中。//2022/01/21/f0870eb3-4714-4fae-9fc3-35e72202f193.jpgStringobjectName=fileUrl;//创建一个OSSClient实例。OSSossClient=newOSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);//删除文件或目录。如果要删除目录,则该目录必须为空。ossClient.deleteObject(bucketName,objectName);//关闭OSSClient.ossClient.shutdown();}}publicclassSmsTemplate{privateSmsProperties属性;publicSmsTemplate(SmsPropertiesproperties){this.properties=properties;}publicvoidsendSms(Stringmobile,Stringcode){System.out.println(properties.getName()+"=="+mobile+"===="+code);}}2.4、AutoConfiguration@EnableConfigurationProperties({SmsProperties.class,OssProperties.class})publicclassCommonAutoConfig{@BeanpublicSmsTemplatesmsTemplate(SmsPropertiessmsProperties){returnnewSmsTemplate(smsProperties);}@BeanpublicOssTemplateossTemplate(OssPropertiesossProperties){returnnewOssTemplate(ossProperties);}}2.5、在资源目录下写入spring.factories,创建一个META-INF文件夹,在META-INF文件夹下创建一个spring.factories文件,内容为org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com。nzc.CommonAutoConfig如果有多个:org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.nzc.CommonAutoConfig\com.xxx.xxx这一步之后我们将这个工程打成Jar包,然后在要使用的工程中引入2.6.应用测试1、创建一个SpringBoot启动类。只有有了启动类才能测试,否则没有上下文~2。编写配置文件spring:application:name:app-servernzc:sms:name:ningzaichunoss:accessKey:xxxsecret:xxxendpoint:oss-cn-shenzhen.aliyuncs.combucketName:xxxurl:xxx修改即可oss的正确配置~编写测试类:@RunWith(SpringRunner.class)@SpringBootTest(classes=AppServerApplication.class)publicclassTemplateTest{@AutowiredprivateOssTemplateossTemplate;@TestpublicvoidtestOss(){Strings=ossTemplate.test();System.out.println(s);}@TestpublicvoidtestUpload(){try{Filefile=newFile("D:\evectionflow01.png");InputStreaminputStream=newFileInputStream(文件);ossTemplate.upload("123.jpg",inputStream);}catch(FileNotFoundExceptione){e.printStackTrace();}}@AutowiredprivateSmsTemplatesmsTemplate;@TestpublicvoidtestSendSms(){smsTemplate.sendSms("17670090715","123456");}}证明是可以用的~后记比较简单易懂~,大家应该能看懂吧,哈哈,如果文中有错误或者错误,欢迎大家评论,我会在时间,非常感谢!这次我可能是在胡说八道。以前我觉得,工作了三四年的Java开发人员,应该懂得很多东西。因为是后端开发,很多时候难免会接触到一些其他的东西。运维部署、测试、前端、底层、架构等等,我说的是接触,不是熟练~,别骂我。只是个人意见。因为我觉得三年是很长很长的时间,也不敢说自己掌握了很多,但是三四年,应该在一个方面或多或少,一定会有比较大的收获。不过有时候平躺的时候真的很平躺~当然可能是习惯了被人卷起来,bb会偷懒一段时间,别怪~