SpringBoot-03-自定义Starter


(实现一个自己的启动器)

命名规约

官方命名:

  • 前缀:spring-boot-starter-xxx

  • 比如:spring-boot-starter-web....

自定义命名:

  • xxx-spring-boot-starter

  • 比如:mybatis-spring-boot-starter


开搞

新建一个空项目spring-boot-starter-diy

新建maven模块 xiaoming-spring-boot-starter

新建一个Springboot模块 xiaoming-spring-boot-starter-autoconfigure

在 starter 中导入 autoconfigure 的依赖

复制代码
<!-- 启动器 -->
<dependencies>
    <!-- 聚合jar 不暴露太多 引入自动配置模块 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>xiaoming-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
复制代码

将 autoconfigure 项目下多余的文件都删掉

pom.xml中只留下spring-boot-starter

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

接口HelloService

复制代码
public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix() + name + helloProperties.getSuffix();
    }

}
复制代码

HelloProperties(和配置文件绑定)

复制代码
// 前缀 xiaoming.hello
@ConfigurationProperties(prefix = "xiaoming.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;
    }
}
复制代码

HelloServiceAutoConfiguration容器处理整合绑定

复制代码
@Configuration
@ConditionalOnWebApplication //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}
复制代码

resources下新建 META-INF/spring.factories交给容器

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.xiaomingspringbootstarterautoconfigure.HelloServiceAutoConfiguration

打包/安装


新建项目测试写好的启动器

新建一个SpringBoot 项目,导入上文写好的启动器

<dependency>
    <groupId>org.example</groupId>
    <artifactId>xiaoming-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

编写一个 HelloController 测试上文的接口

复制代码
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello("zxc");
    }

}
复制代码

配置文件 application.properties

xiaoming.hello.prefix="ppp"
xiaoming.hello.suffix="sss"

启动项目进行测试 http://localhost:8080/hello

测试成功丶

关于HelloService/HelloProperties/HelloServiceAutoConfiguration这三个类,还需要再项目中理解丶


 

posted @   夜雨秋池  阅读(208)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
点击右上角即可分享
微信分享提示