【五一创作】spring boot starter 编写自己的starter

spring boot starter 编写自己的starter

chen
熟悉创建springboot-starter的过程,有利于加深对springboot项目架构的理解,建议大家亲自写写
2023-5-1

源码地址: https://gitcode.net/qq_39339588/my-spring-boot-starter.git

一、封装my-spring-boot-starter

  1. 新建springboot工程,来封装为自己的spring-boot-starter

在这里插入图片描述

  1. 包名,随便写,这里没有什么限制

    版本号设置为0.0.1,方便后期依赖使用

在这里插入图片描述

  1. 依赖,选择基础的几个就行

在这里插入图片描述

  1. 项目是用来创建封装自己的starter,不需要打包插件,删除pom.xml中无用的内容

在这里插入图片描述

  1. 注意看lombok依赖,去掉optional

      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <!--  这个去掉,不然会不向下传递-->
         <!-- <optional>true</optional>-->
      </dependency>
    
  2. 项目是用来创建封装自己的starter,删除启动类和测试相关的

在这里插入图片描述

  1. 只剩下了空架子,如图

在这里插入图片描述

  1. 创建 自定义自动配置类,一会儿在spring.factories文件中会设置引导
package space.xx.xxx.myspringbootstarter.config;

import org.springframework.context.annotation.Configuration;

/**
* 我的-自定义的自动装配类;会由spring.factories文件引导
*
* @author chenzhao
* @create 2023-05-01 17:35
*/
@Configuration
public class MyEnableAutoConfiguration {
   //TODO 一会儿添加内容
}

  1. 创建spring.factories引导文件
    resources文件夹下,新创建META-INF文件夹,在其中新建spring.factories文件
    配置指向"MyEnableAutoConfiguration"类

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=space.xx.xxx.myspringbootstarter.config.MyEnableAutoConfiguration
    

  1. 创建配置文件的类MyProperties, 别的工程依赖后,会读取别的工程中my开头的配置文件
package space.xx.xxx.myspringbootstarter.config;

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

/**
* 配置文件类,装配数据
* @author chenzhao
* @create 2023-05-01 17:45
*/
@Data
@ConfigurationProperties(prefix = "my")
public class MyProperties {
   private String name;
   private Integer age;
}
  1. 创建service层,使用配置类的配置参数
package space.xx.xxx.myspringbootstarter.service;

import lombok.AllArgsConstructor;

/**
* 封装service,方便其他注入使用
* @author chenzhao
* @create 2023-05-01 14:40
*/
@AllArgsConstructor
public class MyService {
   private String name;
   public String sayName(){
       return this.name;
   }
}
  1. 编辑完善MyEnableAutoConfiguration,引入MyProperties.class,注入MyService
package space.xx.xxx.myspringbootstarter.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import space.xx.xxx.myspringbootstarter.service.MyService;

/**
* 我的-自定义的自动装配类;会由spring.factories文件引导
*
* @author chenzhao
* @create 2023-05-01 17:35
*/
@Configuration
@EnableConfigurationProperties(MyProperties.class)
@ConditionalOnProperty("my.name")
public class MyEnableAutoConfiguration {
   /**
    * 装配 MyService
    * @param myProperties
    * @return
    */
   @Bean
   public MyService my2Service(MyProperties myProperties){
       return new MyService(myProperties.getName());
   }
}

  1. 部署到本地maven仓库install一下,会把创建的my-starter打包到maven仓库,其他工程就可以找到这个了。

在这里插入图片描述

二、新建其他spring-boot项目,依赖验证

  1. 新建springboot工程,默认不依赖任何包

  2. 依赖my-spring-boot-starter包

    <dependencies>
       <dependency>
           <groupId>space.xx.xxx</groupId>
           <artifactId>my-spring-boot-starter</artifactId>
           <version>0.0.1</version>
       </dependency>
    </dependencies>
    
  3. 配置文件中application.properties,设置name

    my.name=goldchen
    
  4. 编写测试接口

    package space.goldchen.my.mytest.rest;
    
    import lombok.RequiredArgsConstructor;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import space.xx.xxx.myspringbootstarter.service.MyService;
    
    /**
     * @author chenzhao
     * @create 2023-05-01 12:05
     */
    @RestController
    @RequiredArgsConstructor
    public class HiRest {
        private final MyService myService;
    
        @GetMapping("hi")
        public String say(){
            return  myService.sayName();
        }
    }
    
    
  5. 调用接口,MyService可以直接注入使用了,并且配置中的name也读取过来了
    在这里插入图片描述

  6. 大功告成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Goldchenn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值