千家信息网

SpringBoot 中怎么自定义starter

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇文章为大家展示了SpringBoot 中怎么自定义starter,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1 先创建一个空项目(自己起名字)2 在1步
千家信息网最后更新 2026年10月31日SpringBoot 中怎么自定义starter

本篇文章为大家展示了SpringBoot 中怎么自定义starter,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1 先创建一个空项目(自己起名字)

2 在1步骤中创建的空项目中创建两个模块,创建方式如下图所示

1)创建启动器模块用maven项目我的启动器名称为 mao-spring-boot-starter

 pom.xml如下所示  

     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">         4.0.0com.mao.startermao-spring-boot-starter1.0-SNAPSHOT    UTF-8            UTF-8            1.8                                          com.mao-starter                    mao-spring-boot-starter-autoconfigurer            0.0.1-SNAPSHOT                            

2) 创建自动配置模块,我创建的名字为:mao-spring-boot-starter-autoconfigurer如下图所示![](https://oscimg.oschina.net/oscnet/b7c0a779402394cb5783f3f2eae7a5ce0bc.jpg)pom.xml如下所示

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">         4.0.0    org.springframework.boot            spring-boot-starter-parent            1.5.21.RELEASE                     com.mao-startermao-spring-boot-starter-autoconfigurer0.0.1-SNAPSHOTmao-spring-boot-starter-autoconfigurerDemo project for Spring Boot    UTF-8            UTF-8            1.8        

                    org.springframework.boot                        spring-boot-starter                            

3 创建如下图的3个类

helloProperties.java //属性类

package com.maostarter.mao;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "map.hello")

public class helloProperties {

public String prefix;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;        }private String suffix;

}

HelloService.java //向外提供服务的类

public class HelloService {

helloProperties helloPropertie;public helloProperties getHelloPropertie() {    return helloPropertie;}public void setHelloPropertie(helloProperties helloPropertie) {    this.helloPropertie = helloPropertie;}public String sayHelloMao(String name){    return helloPropertie.getPrefix()+'*'+name+helloPropertie.getSuffix();}

}

helloServiceAutoConfiguration.java //服务自动注入类

@Configuration

@ConditionalOnWebApplication //web应用才生效

@EnableConfigurationProperties(helloProperties.class)

public class helloServiceAutoConfiguration {

@AutowiredhelloProperties helloPropertie;@Beanpublic HelloService helloService(){    HelloService service = new HelloService();            service.setHelloPropertie(helloPropertie);            return  service;}

}

4 配置spring.factories文件,如下图创建方式

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.maostarter.mao.helloServiceAutoConfiguration

5 如何生成starter,在这个过程有肯能出现各个问题,一般是jdk版本不对,maven版本问题,出现问题首先仔细阅读控制台的信息

不要一出现问题就百度

6 测试刚刚创建的自定义starter创建个springBoot项目

pom.xml如下所示

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">         4.0.0    org.springframework.boot            spring-boot-starter-parent            2.1.6.RELEASE                     com.autostart.testautostart0.0.1-SNAPSHOTautostartDemo project for Spring Boot    1.8                    org.springframework.boot                        spring-boot-starter-web                                                com.mao.starter                        mao-spring-boot-starter                        1.0-SNAPSHOT                                                org.springframework.boot                        spring-boot-starter-test                        test                                                                            org.springframework.boot                                    spring-boot-maven-plugin                                                            

6 给之前配置的属性文件复制 如下图所示

7 创建一个测试用例

@Controller

public class HelloController {

@Autowired HelloService helloService; @ResponseBody@RequestMapping("/hello")public String Hello(){    return helloService.sayHelloMao("haha");        }

}

8 测试结果如下图所示

上述内容就是SpringBoot 中怎么自定义starter,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

问题 项目 UTF-8 模块 测试 配置 内容 启动器 属性 技能 文件 方式 版本 知识 中创 服务 不对 简明 简明扼要 两个 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 博彦科技互联网金融 广州万易互联网科技有限公司 美国互联网科技巨头监管 互联网科技战争 狮子座互联网科技培训 互联网科技论坛 深圳卡尔迅互联网科技有限公司怎么样 北京三清互联网科技 互联网科技寓意 金融科技与互联网金融相同和不同 华服互联网科技官网 湖南诚度互联网科技有限公司 岳阳华臻互联网科技有限公司 晓花互联网科技有限公司网址 无锡工业互联网科技有限公司 合肥互联网科技公司公章图片 高科技互联网套路 数字互联网科技有限公司 互联网科技对化妆品行业 华人运通互联网科技公司 精品咖啡豆 互联网科技工作委员会 紫鲸互联网科技 撒么互联网科技公司 华中科技有哪些互联网大佬 老年人脱离互联网科技的作文 博兴的互联网电子科技公司 荆州市昕露互联网络科技有限公司 雨阳互联网卡科技有限公司 互联网的黑科技实验室
0