SpringBoot自定义starter

前言

作为 Java 程序员,SpringBoot 可以说是开发中必不可少的框架。在 SpringBoot 中有各种各样的 starter ,官方的或三方的,今天给大家带来自定义 starter 的教程,动手给自己造个轮子,把自己常用的工具方法放里面,随引随用,告别重复写工具类。

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

搬运代码

接下来就是要把公用的工具类都搬运到 starter 项目里,这里以下面的工具类为例

package com.pine.mytools.util;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author pine
 */
@Data
@NoArgsConstructor
public class PrintUtil {

    private String name;

    private int age;

    public PrintUtil(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void print() {
        System.out.println(name + ": " + age);
    }

}

配置类

接下来写配置类,主要是把一些 bean 注入到 IOC 容器里,以上面的工具类为例

package com.pine.mytools.config;

import com.pine.mytools.util.PrintUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author pine
 */
@Configuration
@ConfigurationProperties("my.tools")
@ComponentScan
@Data
public class MyToolsConfig {
    private String name;
    private int age;

    @Bean
    public PrintUtil printUtil() {
        return new PrintUtil(name, age);
    }
}

配置文件

接下来需要写一个配置文件,这个配置文件的路径和文件名都是固定的,必须是 resources/META-INF/spring.factories

在里面配置好我们上面写的配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.pine.mytools.config.MyToolsConfig

打包

在打包之前有一个坑,就是需要在 pom 文件的打包插件里添加一个配置,如下,否则会报一个找不到主类的异常

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

测试

新建一个测试项目,引入我们的 starter

<dependency>
    <groupId>com.pine</groupId>
    <artifactId>mytools-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

在 application.yml 中配置

my:
  tools:
    name: 张三
    age: 23

编写测试代码

package com.pine.startertest;

import com.pine.mytools.util.PrintUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class StarterTestApplicationTests {

    @Resource
    private PrintUtil printUtil;

    @Test
    void contextLoads() {
        printUtil.print();
    }

}

输出
在这里插入图片描述

结语

好的,大功告成!这样我们就完成了一个极简的 starter,只需要把常用的工具类搬进去,那我们就有了一个自己的 SDK 。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值