自定义Starter

自定义Starter

建立一个项目。

建立两个模块。

一个是starter,启动配置 引用自动配置包。

一个是autoconfigure,

比如Spring-boot-web-starter。

点进去发现,没有源代码。因为他只是一个暴露借口,真正代码写在AutoConfigure.

image-20220512104149399

需要配置

META-INF

下面

spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration

image-20220512104314037

starter模块,pom去引入autoconfigure。

starter作为暴露给外部用的。这个可以用 maven项目。

autoconfigure,这个模块。用Spring Initializr 来做。不启动 不单独打包。

开始

1、新建一个junlin-spring-boot-starter 空项目。

empty project.里面新建两个模块。 maven项目junlin-spring-boot-starter,Spring Initializr 的junlin-spring-boot-starter-autoconfigure
在这里插入图片描述

2、starter模块,pom依赖引用autoconfigure

在这里插入图片描述

pom文件,test 依赖,build下面依赖的插件 都可以干掉。

配置文件,applciation.peroperiese 也可以删掉。

image-202205121100332134、新建两个类,一个实现方法类。一个自动配置类。

PrintAutoConfigure 自动配置类

@Configuration
@ConditionalOnClass(value = {PrintServiceImpl.class} )
public class PrintAutoConfigure {

    @Bean
    @ConditionalOnMissingBean
    PrintServiceImpl startService() {
        return new PrintServiceImpl();
    }
}

PrintServiceImpl 实现方法类

public class PrintServiceImpl  {

    public void printMethod(){
        System.out.println("你好,李君临!");
    }
}

可以打包了。

autoconfigure ,需要添加factories

需要将这个删除

层级关系
——springboot-customer-starter (自定义starter)
————springboot-mystarter (启动器)
————springboot-mystarter-autoconfiguration(自动配置)
解决办法:
将springboot-mystarter-autoconfiguration中的

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

正确导入和 配置 yml文件就可以

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值