其他分享
首页 > 其他分享> > 【SpringBoot】20、自定义启动器 Starter【保姆级教程】

【SpringBoot】20、自定义启动器 Starter【保姆级教程】

作者:互联网

1、说明

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

命名归约:

官方命名:

自定义命名:

2、编写启动器

1)新建一个空项目

在这里插入图片描述

在这里插入图片描述

2)新建一个普通Maven模块

tuwer-spring-boot-starter 这就是自定义启动器的名称,别处引用时就用这个名称

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3)新建一个Springboot模块

在这里插入图片描述

4)基本结构

在这里插入图片描述

5)模块绑定

在普通Maven模块中加Springboot模块的依赖

<dependency>
    <groupId>com.tuwer</groupId>
    <artifactId>tuwer-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

在这里插入图片描述

6)清理Springboot模块

为保持项目干净,建议删除多余部分;不删也能使用

在这里插入图片描述

spring-boot-starter 是所有 starter 的基础依赖,必须要有

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

7)编写自已的服务

在SpringBoot模块中编写自已的服务类

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();
    }
}

8)编写属性配置类

如果不需要调用方配置属性的话,可以省略

@ConfigurationProperties(prefix = "tuwer.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;
    }
}

9)编写自动配置类

指明自已的服务类在什么情况下生效,满足生效条件时,自动注入到Spring容器中,供调用方使用

@Configuration // 自动配置类
@ConditionalOnWebApplication // 是web应用时生效
@EnableConfigurationProperties(HelloProperties.class) // HelloProperties存在时生效
public class HelloServiceAutoConfiguration {

    /**
     * 注入属性配置类
     */
    @Autowired
    HelloProperties helloProperties;

    /**
     * 生成服务类,注入到容器中
     * @return
     */
    @Bean
    public HelloService getHelloService(){
        HelloService helloService = new HelloService();
        // 设置属性
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }

}

10)编写spring.factories

指明自动配置类的地址

resources 目录下编写一个自己的 META-INF\spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tuwer.config.HelloServiceAutoConfiguration

在这里插入图片描述

11)Install到仓库

安装到maven本地仓库中

在这里插入图片描述

在这里插入图片描述

3、测试启动器

1)新建一个SpringBoot 项目

2)导入自己的启动器

<dependency>
    <groupId>com.tuwer</groupId>
    <artifactId>tuwer-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

3)编写 HelloController

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

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

4)编写配置文件 application.properties

tuwer.hello.prefix=1111111 
tuwer.hello.suffix=2222222

5)启动测试

在这里插入图片描述

在这里插入图片描述

4、自定义日志拦截启动器

在这里插入图片描述

标签:helloProperties,启动器,SpringBoot,自定义,spring,boot,tuwer,public,starter
来源: https://blog.csdn.net/tu_wer/article/details/122715770