本文档根据SpringBoot Starter开发规范自定义HkBootStarter意在学习如何开发SpringBoot Starter。Maven POM.xmlprojectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactId!--version3.1.2/version--version2.3.5.RELEASE/version/parentgroupIdcom.hk.eshop/groupIdartifactIdHkBootStarter/artifactIdversion1.0-SNAPSHOT/versionpackagingjar/packagingnameHkBootStarter/nameurlhttp://maven.apache.org/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion3.8.1/versionscopetest/scope/dependency/dependencies!-- 插件 --buildresourcesresourcedirectorysrc/main/java/directoryincludesinclude**/*.properties/includeinclude**/*.xml/include/includesfilteringtrue/filtering/resourceresourcedirectorysrc/main/resources/directoryincludesinclude**/**/include/includesfilteringfalse/filtering/resource/resources/build/project注解EnableHkConfigurationpackagecom.hk.eshop;importjava.lang.annotation.*;Target({ElementType.TYPE})Retention(RetentionPolicy.RUNTIME)DocumentedpublicinterfaceEnableHkConfiguration{}监听HkApplicationListenerpackagecom.hk.eshop;importorg.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;importorg.springframework.context.ApplicationListener;publicclassHkApplicationListenerimplementsApplicationListenerApplicationEnvironmentPreparedEvent{OverridepublicvoidonApplicationEvent(ApplicationEnvironmentPreparedEventapplicationEnvironmentPreparedEvent){}}配置信息类HkPropertiespackagecom.hk.eshop;importorg.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefixspring.hk)publicclassHkProperties{privateStringnamenull;}初始化类HkConfigurationpackagecom.hk.eshop;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.autoconfigure.condition.ConditionalOnBean;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationConditionalOnBean(annotationEnableHkConfiguration.class)EnableConfigurationProperties(HkProperties.class)publicclassHkConfiguration{privateStringLINE_SEPARATORSystem.getProperty(line.separator);privatestaticStringhkBanner#################################################################\n# ## ## ## ## #\n# ## ## ## ## #\n# ######### #### #\n# ## ## ## ## #\n# ## ## ## ## SpringBootStarter #\n#################################################################;AutowiredprivateHkPropertieshkProperties;publicHkConfiguration(){System.out.println(buildBannerText());}BeanpublicHkTestBeancreateTestBean(){System.out.println(hk.propertieshkProperties);returnnewHkTestBean();}privateStringbuildBannerText(){StringBuildersbnewStringBuilder();sb.append(LINE_SEPARATOR).append(hkBanner).append( :: Hk :: (v1.0.0)).append(LINE_SEPARATOR);returnsb.toString();}}测试类HkTestBeanpackagecom.hk.eshop;importjava.io.*;importjava.util.List;importjava.util.ArrayList;publicclassHkTestBean{privateListStringfileListnewArrayList();publicListStringgetFileList(Stringpath){recurseFile(path);returnfileList;}privatevoidrecurseFile(Stringpath){FilefnewFile(path);if(f.isDirectory()){File[]filesf.listFiles();for(Fileff:files)recurseFile(ff.getAbsolutePath());}elsefileList.add(f.getAbsolutePath());}}/resources/META-INF/spring.factories文件org.springframework.boot.autoconfigure.EnableAutoConfiguration\com.hk.eshop.HkConfigurationorg.springframework.context.ApplicationListener\com.hk.eshop.HkApplicationListener编译安装到本地Maven仓库mvn installSpringBoot项目引入HkBootStarter模块