springboot-自定义starter

2 篇文章 0 订阅

springboot的Starter?

starter是啥?
starter有啥用?
怎么自定义starter?
针对于这些,我也不知道!哈哈哈!但是经过一番折腾后,有个大概了解,有不对之处,多多指教!

starter是啥?

其实啥也不是,就是一堆jar包的集合,你引用我,我引用你,然后就有一大堆jar出现了,也就是个maven工程,之后还是要放到公司私服给其他同事使用(我的想法是写starter给其他公司用,哈哈哈),大概也就是一个pom的maven工程,然后可以让别人引入!!

starter有啥用?

这玩意还是有点用的:

  1. starter可以将配置文件相关的东西全部弄好,在引入到springboot的时候通过spi机制,就可以扫描到能正常使用的bean了。所以说,Spring Boot 是简化配置。
  2. starter可以配置好与spring整合相关的配置和相关依赖(jar和jar版本),不用去考虑版本的问题,使用者引入就可以使用。

怎么自定义starter?

写代码才好玩,才有意思,开始进入主题!!

首先需要创建两个工程:

  1. 一个是starter上传
  2. 一个是starter的使用
自定义starter工程

no bb!看图:
工程测试

  • 一个是starter
  • 一个是autoconfigure

为啥需要这样创建呢,我也不知道,啊哈哈,学人家springboot官方的,其实也能理解,starter就是个空的项目,只需要引入autoconfigure即可,这样类似于多态的形式,易于扩展

==》看看starter里面有啥:
创建项目过程就不演示了,首先是创建一个空的工程,然后分别创建两个模块,这里空工程最好不要创建成maven工程,不需要统一版本管理;
空工程
上图那样就可以创建出空工程了!!

继续回到starter模块,其实就一个地方有改动,pom文件:

<?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>

    <groupId>com.simple</groupId>
    <artifactId>simple-spring-boot-starter</artifactId>
    <version>1.0.0.RELEASE</version>

    <dependencies>
        <dependency>
            <groupId>com.simple</groupId>
            <artifactId>simple-spring-boot-starter-autoconfigure</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
    </dependencies>

</project>

我们其实可以看到,就是引入了autoconfigure模块,自己本身没有什么逻辑,所以没了。简单不,那就看看下一个吧!!

自定义autoconfigure工程

自动配置

看上图,其实也很简单,就是对外提供一个HelloService服务,我把三个文件全部粘贴出来(这些项目在我gitee上面全部都有,还有很多其他的项目)

  • 首先看看配置类:HelloProperties.java
package com.simple.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 项目: starter-test
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2021-05-16 21:56
 **/
@ConfigurationProperties(prefix = "simple.hello")
@Component
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;
    }
}

文件很简单,就是有个前缀和后缀,这个参数可以在yaml文件或者properties文件中进行配置,之后使用这个starter的时候会说

  • 再看看HelloService.java
package com.simple.starter;

/**
 * 项目: starter-test
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2021-05-16 21:56
 **/
public class HelloService {

    HelloProperties helloProperties;

    public HelloService(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

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

}

这里就是一个很简单的逻辑,往传进来的字符串前后拼接前面说的前后缀;

  • 再看看比较核心的:HelloServiceAutoConfiguration.java
package com.simple.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 项目: starter-test
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2021-05-16 21:57
 **/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        return new HelloService(helloProperties);
    }
}

注意: 这里有个注解 @ConditionalOnWebApplication,这个含义就是需要在web环境下才会生效!!!

这个文件就是能给其他工程提供服务的关键之一,这里就相当于把HelloService装配好,然后供使用者使用,但是有个问题,那这个配置文件怎么起作用??这个配置文件Springboot怎么知道他在哪??要知道他在哪才知道有HelloService??

所以还有个关键文件:spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.simple.starter.HelloServiceAutoConfiguration

这个文件需要放在resources -> META-INF目录下才能生效!!

为什么放在这里才能生效了??我偷偷看了人家官方写的,都是放在那的,哈哈哈!!

其实不难,这个跟springboot自动加载的过程有关,也是springboot的启动过程有关,这里就不详细展开讲了,可以在网上找一篇博文看看(等我有时间,也来奉献一篇)

然后在看看pom文件:

<?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>

    <groupId>com.simple</groupId>
    <artifactId>simple-spring-boot-starter-autoconfigure</artifactId>
    <version>1.0.0.RELEASE</version>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

一个引入boot相关的,另外一个是结局HelloProperties文件报红的依赖,可以去掉试试!

到此两个工程就算是做完了,也就是我们自己的starter算是写完了;然后将两个项目进行安装,到自己本地仓库(也可以推送到自己的私服,或者maven的共有仓库):
安装
这里注意,每次install时候,最好先安装被依赖的那个工程先,即autoxxx;

至此我们的starter就算安装完了!!!GO!!开始测试使用!

怎么使用自定义的starter?

测试
首先创建一个工程,看看其pom文件:

<?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 https://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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.simple.starter</groupId>
    <artifactId>starter-test</artifactId>
    <version>1.0.0.RELEASE</version>
    <name>starter-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!-- 引入自定义的starter -->
        <dependency>
            <groupId>com.simple</groupId>
            <artifactId>simple-spring-boot-starter</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

引入我们自定义的starter,如果没有提示的话,就重新安装一下starter项目,或者直接从starter项目那里直接粘贴过来:
在这里插入图片描述
然后在来写个代码测试一下:

package com.simple.starter.controller;

import com.simple.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目: starter-use-test
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2021-05-16 22:50
 **/
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

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

然后我在 application.properties文件中也进行了设置:

simple.hello.prefix=Prefix-Hello
simple.hello.suffix=Suffix-World

万事具备,只差测试,哈哈哈;启动项目!!
然后在浏览器中输入:http://127.0.0.1:8080/hello
看看效果:
效果

哈哈哈,可以看到我们项目已经引入了,可以正常使用了!!

总结

这只是定义了一个很简单的starter,实际开发中要结合业务需求之类的再去自定义自己的starter,常见于 redis的starter 各种支付、登录的sdk等!!分享就到这里了!


感谢大家阅读、互相学习;
有问题评论或者发邮箱;
gitee:很多代码仓库;
1449697757@qq.com

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
自定义Spring Boot Starter是一种用于简化Spring Boot应用程序配置和依赖管理的机制。它由几个组件组成,包括starter包、autoconfiguration包和配置文件。 首先,你需要创建一个Maven工程,并创建三个模块。其中,starter包负责引入依赖,autoconfiguration包负责实现装配。在autoconfiguration包中,你需要定义对应的properties类、configuration类和核心业务类。此外,你还需要在autoconfiguration包的/resources/META-INF/目录下添加spring.factories文件,并配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.qiejk.demo.springboot.autoconfiguration.DemoAutoConfiguration。这一步是Spring Boot装配的核心,只有添加了这个内容,Spring Boot才会进行自动装配。\[1\] 在父模块的pom文件中,你需要引入Spring Boot的依赖,例如spring-boot-starter-web,以及指定Spring Boot的版本。这样,你的自定义starter模块就可以基于Spring Boot进行开发和使用。\[2\] 最后,你需要创建一个配置文件,用于告诉Spring Boot在启动时扫描该配置文件,并将其中的配置类加载到Spring容器中。这个配置文件的作用是指定自动配置类的位置。\[3\] 通过以上步骤,你就可以创建自定义Spring Boot Starter,并在应用程序中使用它来简化配置和依赖管理。 #### 引用[.reference_title] - *1* [如何自定义springboot-starter](https://blog.csdn.net/sinat_29434159/article/details/123995794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot自定义Starter篇](https://blog.csdn.net/m0_46571920/article/details/122910726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值