网站流量提供商,2014网站建设,软件工程4大证书,手机怎么制作自己的网站【README】
本文po出了自建springboot 启动器步骤#xff1b; 【1】新建2个starter相关组件
根据 mybatis-spring-boot-starter#xff0c;我们看到 自建starter需要两个组件#xff0c;分别是 xxx-spring-boot-starter#xff0c; xxx-spring-boot-starter-autoconfigu…【README】
本文po出了自建springboot 启动器步骤 【1】新建2个starter相关组件
根据 mybatis-spring-boot-starter我们看到 自建starter需要两个组件分别是 xxx-spring-boot-starter xxx-spring-boot-starter-autoconfigure
其中xxx-spring-boot-starter负责引入 xxx-spring-boot-starter-autoconfigure 依赖 xxx-spring-boot-starter-autoconfigure 负责定义相关配置
step1新建空项目 项目名称为 springbt-08-starter-diy2 step2在空项目上新建2个组件分别为
lisi-spring-boot-starter 和 lisi-spring-boot-starter-autoconfigure
其中 lisi-spring-boot-starter为 单纯的maven项目 lisi-spring-boot-starter-autoconfigure 通过 spring Initializer 工具创建 step3 lisi-spring-boot-starter 引入 lisi-spring-boot-starter-autoconfigure 依赖
lisi-spring-boot-starter组件的pom.xml 如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://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/modelVersiongroupIdcom.cmc.starter/groupIdartifactIdlisi-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/versiondependencies!--引入 lisi-spring-boot-starter-autoconfigure 依赖--dependencygroupIdcom.cmc.starter/groupIdartifactIdlisi-spring-boot-starter-autoconfigure/artifactIdversion0.0.1-SNAPSHOT/version/dependency/dependencies/project
并把测试目标目录删除 lisi-spring-boot-starter目录结构如下 step4 lisi-spring-boot-starter-autoconfigure 组件的pom.xml 如下
删除多余的插件 测试库依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.5/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.cmc.starter/groupIdartifactIdlisi-spring-boot-starter-autoconfigure/artifactIdversion0.0.1-SNAPSHOT/versionnamelisi-spring-boot-starter-autoconfigure/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency/dependencies/project【2】开发 starter-autoconfigure 启动器配置组件
step1 新建 HelloServiceHelloProperties HelloServiceAutoConfiguration
package com.cmc.starter.lisi;import org.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefix cmc.hello)
public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix suffix;}
}package com.cmc.starter.lisi;public class HelloService {HelloProperties helloProperties;public String sayHelloCmc(String name) {return helloProperties.getPrefix() - name - helloProperties.getSuffix();}public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties helloProperties;}
}package com.cmc.starter.lisi;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
ConditionalOnWebApplication // web应用才生效
EnableConfigurationProperties(HelloProperties.class) // 使属性文件生效
public class HelloServiceAutoConfiguration {AutowiredHelloProperties helloProperties;Beanpublic HelloService helloService() {HelloService helloService new HelloService();helloService.setHelloProperties(helloProperties);return helloService;}
}step2在资源类路径下新建 META-INF/spring.factories 属性文件添加 自动配置类全限定类名
# spring.factories# Auto Config
org.springframework.boot.autoconfigure.EnableAutoConfiguration\
com.cmc.starter.lisi.HelloServiceAutoConfiguration
参考的是 spring-boot-autoconfigure.jar 中的 META-INF/spring.factories的内容 目录结构如下 step3先把 lisi-spring-boot-starter-autoconfigure 制品库安装到本地仓库
step4再把 lisi-spring-boot-starter 制品库安装到本地仓库 【3】自建starter使用
step1 新建项目只需要引入 lisi-spring-boot-starter依赖 即可 因为 lisi-spring-boot-starter 引入了 lisi-spring-boot-starter-autoconfigure传递依赖
pom.xml 如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.5/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.cmc/groupIdartifactIdspringboot-08-starter-test2/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot-08-starter-test2/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependencies!--引入自定义的starter--dependencygroupIdcom.cmc.starter/groupIdartifactIdlisi-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/version/dependency!-- 测试库 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!-- springboot web库 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/projectstep2 新建测试项目编写 Controller引入 HelloService
import com.cmc.starter.lisi.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class HelloController {AutowiredHelloService helloService;GetMapping(/hello)public String hello() {return helloService.sayHelloCmc(zhangsan);}
}
step3HelloServiceAutoConfiguration有两个属性前缀后缀需要配置在 application.properties 中配置如下
cmc.hello.prefixchengdu
cmc.hello.suffix028
step4启动springboot项目并访问
localhost:8080/hello ;