其他分享
首页 > 其他分享> > 自己如何写一个 SpringBoot 第三方依赖包?

自己如何写一个 SpringBoot 第三方依赖包?

作者:互联网

本文 SpringBoot 为 2.3.4版本

1、SpringBoot特点,了解自动配置原理

1.1、依赖管理

<!-- 依赖管理 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>

		
<!-- ↓↓↓ spring-boot-starter-parent 的父项目 ↓↓↓ -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>

<!-- spring-boot-dependencies依赖,几乎声明了所有在开发中常用的依赖及版本号,自动版本仲裁机制 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.3.4.RELEASE</version>
    <scope>compile</scope>
</dependency>
1、引入依赖默认都可以不写版本,比如:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2、引入非版本仲裁的jar,要写版本号。
1、查看 spring-boot-dependencies 里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置
<properties>
    <mysql.version>5.1.43</mysql.version>
</properties>

1.2、自动配置Web依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
@SpringBootApplication 注解
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")

模拟与SpringBoot整合的第三方依赖

image-20210617115008826

整体结构

starter项目

<dependencies>
    <dependency>
        <groupId>com.calm</groupId>
        <artifactId>calm-hello-spring-boot-autoconfigure</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

autoconfigure项目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>
// 去配置文件中找以“prefix属性值”为前缀的属性,将他们的值绑定到该类上
@ConfigurationProperties(prefix = "calm.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;
    }
}
public class HelloService {
    
    // 注入属性
    @Autowired
    private HelloProperties helloProperties;

    public String sayHello(String name){
        return helloProperties.getPrefix() + "--" + name + "--" + helloProperties.getSuffix();
    }
    
}
@Configuration
// 开启HelloProperties类的配置绑定功能,将绑定好之后放入到IOC容器中。
@EnableConfigurationProperties(HelloProperties.class) 
public class HelloServiceAutoConfiguration {

    // 判断若IOC容器中没有 HelloService类型的Bean,才会将HelloService注册到容器中。
    // 目的是如果用户自定义了一个HelloService,那就用用户自定义的,如果没有自定义,那就用我们的。
    @ConditionalOnMissingBean(HelloService.class)
    @Bean
    public HelloService helloService(HelloProperties helloProperties){
        // 从容器中拿到 HelloProperties 进行设置值。
        helloProperties.setPrefix("注册Bean到容器中。");
        return new HelloService();
    }

}

测试

新建个项目,引入自定义的starter依赖,autoconfigure会自动引入进来,依赖传递。

<dependency>
    <groupId>com.calm</groupId>
    <artifactId>calm-hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
calm:
  hello:
    prefix: 配置前缀
    suffix: 配置后缀
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringBootStarterAutoConfiguration {

    @Autowired
    private HelloService helloService;

    @Test
    public void sayHello(){
        System.out.println(helloService.sayHello("张三"));
    }

}

标签:依赖,SpringBoot,spring,boot,autoconfigure,public,第三方,starter
来源: https://blog.csdn.net/weixin_44414460/article/details/121842641