自定义一个springboot启动器

对于springboot-starter很好理解即让spring容器帮我们自动配置引入的组件。

下面具体讲一下怎么创建自定义的启动器。

首先我们需要用idea新建一个空的项目
在这里插入图片描述
接下来new一个moudle,注意是maven类型的
在这里插入图片描述
要确保新建的moudle要在项目的目录下(注意第三方启动器命名格式:xxxx-spring-boot-starter)
在这里插入图片描述
再建一个moudle,注意类型是spring initializr,同样确保其在项目的目录下
在这里插入图片描述
创建完成后将成后,项目结构
在这里插入图片描述

然后我们可以在这个模块中定义自己的服务

package com.yk.autoconfigure.stat;

import com.yk.autoconfigure.properties.FoolsDayProperties;
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.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Date;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(FoolsDayProperties.class)
@Import(FoolsDayAutoConfiguration.Clfd.class)
public class FoolsDayAutoConfiguration {

    public static class Clfd{
        FoolsDayProperties foolsDayProperties;

        public Clfd(FoolsDayProperties foolsDayProperties) {
            this.foolsDayProperties = foolsDayProperties;
        }
        public void showIf(){
            System.out.println(this.foolsDayProperties.toString());
        }
    }
}
package com.yk.autoconfigure.properties;


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

import java.util.Date;

@ConfigurationProperties("custom.foolsday")
public class FoolsDayProperties {
    private Date date;

    private String weather;

    private String mind;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getMind() {
        return mind;
    }

    public void setMind(String mind) {
        this.mind = mind;
    }

    @Override
    public String toString() {
        return "FoolsDayProperties{" +
                "date=" + date +
                ", weather='" + weather + '\'' +
                ", mind='" + mind + '\'' +
                '}';
    }
}

接下来将配置模块中多余的依赖去掉
在这里插入图片描述
启动类去掉,多余文件去掉,然后新建一个META-INF/spring.factories文件
在这里插入图片描述

spring.factories文件内容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yk.autoconfigure.stat.FoolsDayAutoConfiguration

将配置模块打包
在这里插入图片描述

在fl-spring-boot-starter模块pom文件中引入下面模块的依赖搜索

将其打包
在这里插入图片描述


搞完收工

我们来用一下刚刚定义的启动器,导入依赖
在这里插入图片描述
测试
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ashes of time

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

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

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

打赏作者

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

抵扣说明:

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

余额充值