序言 现在的项目都是以springboot为脚手架进行开发的主要原因是由于springboot有自动装载的功能,通俗的来讲,就是引入对应starter依赖就可以实现相应的功能,那我们如何实现自定义starter呢应用场景登录认证功能 由于项目中都需要登录认证功能,我们可以把登录认证功能抽取,封装成starter,直接引入项目中,就可以实现登录认证功能。公司基础框架 公司公共依赖、响应体,异常等处理,都可以封装成starter创建自定义starter 1创建自定义starter1。1配置pom。xmllt;?xmlversion1。0encodingUTF8?projectxmlnshttp:maven。apache。orgPOM4。0。0xmlns:xsihttp:www。w3。org2001XMLSchemainstancexsi:schemaLocationhttp:maven。apache。orgPOM4。0。0https:maven。apache。orgxsdmaven4。0。0。xsdmodelVersion4。0。0modelVersionparentgroupIdorg。springframework。bootgroupIdspringbootstarterparentartifactIdversion2。7。4versionrelativePath!lookupparentfromrepositoryparentgroupIdcom。gzgroupIdhellospringbootstarterartifactIdversion1。0SNAPSHOTversionnamehellospringbootstarternamedescriptionhellospringbootstarterdescriptionpropertiesjava。version1。8java。versionpropertiesdependenciesdependencygroupIdorg。springframework。bootgroupIdspringbootstarterartifactIddependencydependencygroupIdorg。springframework。bootgroupIdspringbootconfigurationprocessorartifactIdoptionaltrueoptionaldependencydependencygroupIdorg。springframework。bootgroupIdspringbootstarterwebartifactIddependencydependencygroupIdorg。projectlombokgroupIdlombokartifactIddependencydependencygroupIdorg。springframework。bootgroupIdspringbootautoconfigureartifactIddependencydependenciesproject1。2创建servicepublicclassHelloService{privateStringname;privateStringaddress;publicHelloService(Stringname,Stringaddress){this。namename;this。addressaddress;}publicStringsayHello(){return你好!我的名字叫name,我来自address;}}1。3创建配置类属性ConfigurationProperties(prefixhello)DatapublicclassHelloProperties{privateStringname;privateStringaddress;}1。4创建配置类ConfigurationEnableConfigurationProperties(HelloProperties。class)publicclassHelloServiceAutoConfiguration{privateHelloPropertieshelloProperties;通过构造方法注入配置属性对象HelloPropertiespublicHelloServiceAutoConfiguration(HelloPropertieshelloProperties){this。helloPropertieshelloProperties;}实例化HelloService并载入SpringIoC容器BeanConditionalOnMissingBeanpublicHelloServicehelloService(){returnnewHelloService(helloProperties。getName(),helloProperties。getAddress());}}1。5创建METAINFspring。factoriesorg。springframework。boot。autoconfigure。EnableAutoConfigurationcom。gz。hellospringbootstarter。config。HelloServiceAutoConfiguration2。创建测试项目2。1创建controllerRestControllerRequestMapping(hello)publicclassHelloController{HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入AutowiredprivateHelloServicehelloService;GetMapping(say)publicStringsayHello(){returnhelloService。sayHello();}}2。2引入pom。xml!导入自定义starterdependencygroupIdcom。gzgroupIdhellospringbootstarterartifactIdversion1。0SNAPSHOTversiondependency2。3application。ymlhelloname:guozhongaddress:qingdao2。4访问