以Springboot作为微服务开发的项目中会用到很多第三方,或者自建的一些依赖SDK,例如分布式锁、文件管理、数据库连接、统一错误拦截等组件。为了达到通用的目的,都会创建自定义的springbootstater组件。以下介绍创建步骤: 一、创建Maven测试项目teststarterpom文件如下:projectxmlnshttp:maven。apache。orgPOM4。0。0xmlns:xsihttp:www。w3。org2001XMLSchemainstancexsi:schemaLocationhttp:maven。apache。orgPOM4。0。0http:maven。apache。orgxsdmaven4。0。0。xsdparentgroupIdcom。test。startergroupIdteststarterartifactIdversion1。0。0versionparentmodelVersion4。0。0modelVersiontestspringbootstarterartifactIdpackagingjarpackagingdependenciesdependencygroupIdorg。springframework。bootgroupIdspringbootautoconfigureartifactIdversion2。3。2。RELEASEversiondependencydependenciesbuildfinalNametestspringbootstarterfinalNamebuildproject 二、创建自动装配类TestServiceConfiguration。javapackagecom。test。spring。boot。starter。importorg。springframework。beans。factory。annotation。Aimportorg。springframework。boot。context。properties。EnableConfigurationPimportorg。springframework。context。annotation。Bimportorg。springframework。context。annotation。Cimportcom。test。spring。boot。starter。TestSimportcom。test。spring。boot。starter。TestServiceIConfigurationEnableConfigurationProperties(valueTestServiceProperties。class)publicclassTestServiceConfiguration{AutowiredprivateTestServicePBeanpublicTestServicegetTestService(){returnnewTestServiceImpl(properties);}} 三、创建配置类TestServiceProperties。javapackagecom。test。spring。boot。starter。importorg。springframework。boot。context。properties。ConfigurationPimportlombok。Dimportlombok。ToSDataToStringConfigurationProperties(prefixtest。starter)publicclassTestServiceProperties{客户端IDprivateStringclientId;客户端名称privateStringclientN} 四、创建提供服务的接口TestService。javapackagecom。test。spring。boot。publicinterfaceTestService{测试paramnamereturnStringsayHello(Stringname);} 五、创建TestService的实现类型TestServiceImpl。javapackagecom。test。spring。boot。importcom。test。spring。boot。starter。config。TestServiceP服务实现authortestpublicclassTestServiceImplimplementsTestService{privateTestServicePpublicTestServiceImpl(TestServicePropertiesproperties){this。}OverridepublicStringsayHello(Stringname){returnclientId:properties。getClientId(),clientName:properties。getClientName(),welcomename!;}} 六、创建自定义注解EnableTestpackagecom。test。spring。boot。starter。importjava。lang。annotation。Dimportjava。lang。annotation。ElementTimportjava。lang。annotation。Rimportjava。lang。annotation。RetentionPimportjava。lang。annotation。Timportorg。springframework。context。annotation。Iimportcom。test。spring。boot。starter。config。TestServiceC自定义自动装配启动注解authortestRetention(RetentionPolicy。RUNTIME)Target(ElementType。TYPE)DocumentedImport({TestServiceConfiguration。class})publicinterfaceEnableTest{} 通过以上步骤就实现了自定义Springbootstartertestspringbootstarter组件,下面创建测试项目去调用这个starter组件。 创建starter组件使用项目 一、创建maven测试项目testservice,pom文件如下projectxmlnshttp:maven。apache。orgPOM4。0。0xmlns:xsihttp:www。w3。org2001XMLSchemainstancexsi:schemaLocationhttp:maven。apache。orgPOM4。0。0http:maven。apache。orgxsdmaven4。0。0。xsdparentgroupIdcom。test。startergroupIdteststarterartifactIdversion1。0。0versionparentmodelVersion4。0。0modelVersiontestserviceartifactIdpackagingjarpackagingdependenciesdependencygroupIdorg。springframework。bootgroupIdspringbootstarterwebartifactIddependencydependencygroupIdorg。springframework。bootgroupIdspringbootstartertestartifactIdscopetestscopeexclusionsexclusiongroupIdorg。junit。vintagegroupIdjunitvintageengineartifactIdexclusionexclusionsdependency!把自定义组件依赖进来dependencygroupIdcom。test。startergroupIdtestspringbootstarterartifactIdversion1。0。0versiondependencydependenciesbuildfinalNametestservicefinalNamepluginsplugingroupIdorg。springframework。bootgroupIdspringbootmavenpluginartifactIdversion{spring。boot。version}versionexecutionsexecutiongoalsgoalrepackagegoalgoalsexecutionexecutionspluginpluginsbuildproject 二、创建启动类Application。javapackagecom。test。importorg。springframework。boot。SpringAimportorg。springframework。boot。autoconfigure。SpringBootAimportorg。springframework。context。annotation。ComponentSimportcom。test。spring。boot。starter。annotation。EnableTEnableTest启动自定义组件ComponentScan(basePackages{com。test。service})SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String〔〕args){SpringApplication。run(Application。class,args);}} 三、创建配置文件application。ymltest:starter:clientid:testservice01clientname:testservice 四、创建测试类TestController。javapackagecom。test。service。importjavax。annotation。Rimportorg。springframework。web。bind。annotation。GetMimportorg。springframework。web。bind。annotation。PathVimportorg。springframework。web。bind。annotation。RequestMimportorg。springframework。web。bind。annotation。RestCimportcom。test。spring。boot。starter。TestSRestControllerRequestMapping(v1test)publicclassTestController{ResourceprivateTestServicetestSGetMapping({name})publicStringhello(PathVariable(name)Stringname){returntestService。sayHello(name);}} 到这里整个自定义springbootstarter创建就完成了,同时也依赖到项目中进行使用了。 项目代码:startertest:自定义starter组件