springboot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,springboot就能自动扫描到要加载的信息并启动相应的默认配置。
我们将可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,springboot为我们完成自动装配。
比如登录模块,基于aop的日志模块等。
springboot提供的starter以spring-boot-starter-xxx
的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter
命名规则。以区分springboot生态提供的starter。
以下是定义一个starter的步骤:
1. 建立一个父项目
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.9.release</version> </parent> <groupid>org.example</groupid> <artifactid>springboot-starter-demo</artifactid> <version>1.0-snapshot</version> <modules> <module>springboot-starter-config</module> <module>springboot-starter-application</module> </modules> <packaging>pom</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
2. 新建一个maven module项目作为自定义starter项目
pom.xml如下:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactid>springboot-starter-demo</artifactid> <groupid>org.example</groupid> <version>1.0-snapshot</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>springboot-starter-config</artifactid> <version>1.0-snapshot</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional>true</optional> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> </dependencies> </project>
3. 定义属性类
package com.demo; import org.springframework.boot.context.properties.configurationproperties; /** * 描述:配置信息 实体 * **/ @configurationproperties(prefix = "demo") public class demoproperties { private string params1; private string params2; private integer minlength; public string getparams1() { return params1; } public void setparams1(string params1) { this.params1 = params1; } public string getparams2() { return params2; } public void setparams2(string params2) { this.params2 = params2; } public integer getminlength() { return minlength; } public void setminlength(integer minlength) { this.minlength = minlength; } }
4. 定义服务类
package com.demo.service; import com.demo.demoproperties; import org.springframework.beans.factory.annotation.autowired; /** * author:huawei */ public class demoservice { @autowired private demoproperties demoproperties; public string params1; public string params2; public demoservice(string param1, string param2) { this.params1 = param1; this.params2 = param2; } public string paramsinfo() { return this.params1 + "! " + params2; } public boolean isvalidlength(string param) { int length = param.length(); integer minlength = demoproperties.getminlength(); if (length < minlength.intvalue()) { return false; } else { return true; } } }
5. 定义配置类
package com.demo; import com.demo.service.demoservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.autoconfigure.condition.conditionalonproperty; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; /** * 描述:配置类 * **/ @configuration @enableconfigurationproperties(demoproperties.class) @conditionalonproperty( prefix = "demo", name = "enable", havingvalue = "true" ) public class demostarterautoconfig { @autowired private demoproperties demoproperties; @bean(name = "demo") public demoservice demoservice(){ return new demoservice(demoproperties.getparams1(), demoproperties.getparams2()); } }
6. 重要的一步
resource目录下添加meta-inf目录,在其下面建立文件spring.factories,内容如下:
org.springframework.boot.autoconfigure.enableautoconfiguration=com.demo.demostarterautoconfig
经过以上步骤,starter就完成了。
以下是建立测试项目的步骤。
7. 建立maven module测试项目
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactid>springboot-starter-demo</artifactid> <groupid>org.example</groupid> <version>1.0-snapshot</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>springboot-starter-application</artifactid> <version>1.0-snapshot</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.example</groupid> <artifactid>springboot-starter-config</artifactid> <version>1.0-snapshot</version> </dependency> </dependencies> </project>
8. 添加配置文件application.yml
server: port: 8010 demo: enable: true params1: 参数1 params2: 参数2 minlength: 4
9. 添加测试controller
package com.demo.starter.controller; import com.demo.service.demoservice; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; import javax.annotation.resource; @restcontroller public class appcontroller { @resource(name = "demo") private demoservice demoservice; @getmapping("/test") public string test(){ boolean valid = demoservice.isvalidlength("test"); if(valid) return demoservice.paramsinfo(); else return "无效数据"; } }
10. 打开浏览器,执行测试
自此完成了starter的基本测试。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论