Spring Boot 自定义Starter

一、自定义starter

1、创建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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>difa</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

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

</project>

2、定义对外接口

package org.example;

/**
 * Des:对外接口
 *
 * @author RuYi
 * @version 1.0.0
 * @create 2024/6/15 21:01
 */
public interface IMyStarterService {

    String myStarterTest(String value);

}

3、定义对外接口的实现类

package org.example;

/**
 * Des:对外接口的实现类
 *
 * @author RuYi
 * @version 1.0.0
 * @create 2024/6/15 21:01
 */
public class MyStarterServiceImpl implements IMyStarterService {

    @Override
    public String myStarterTest(String value) {
        return value + " My Starter";
    }
}

4、定义spring.factories

在resources目录下创建META-INF目录,在META-INF目录创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.MyStarterAutoConfigure 

5、定义自动配置类

package org.example;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Des:定义自动配置类
 *
 * @author RuYi
 * @version 1.0.0
 * @create 2024/6/15 21:02
 */
@Configuration
// 类路径下发现XXX.class就自动配置
@ConditionalOnClass(value = {IMyStarterService.class, MyStarterServiceImpl.class})
public class MyStarterAutoConfigure {

    @Bean // 实例化IMyStarterService并载入Spring IoC容器
    @ConditionalOnMissingBean
        // 当spring上下文中不存在bean时实现自动配置
    IMyStarterService myStarterService() {
        return new MyStarterServiceImpl();
    }

}

 6、安装Starter到本地仓库

mvn clean install -Dmaven.test.skip=true

 7、最终结果

二、使用自定义的starter 

1、创建普通boot项目,并在pom中,引入starter

<?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.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

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

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

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>difa</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

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

</project>

 2、查看依赖

3、编写测试代码

package com.example.demo;

import org.example.IMyStarterService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * Des:
 *
 * @author RuYi
 * @version 1.0.0
 * @create 2024/6/15 21:51
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class test {

    @Resource
    private IMyStarterService myStarterService;

    @Test
    public void kk() {
        System.out.println(myStarterService.myStarterTest("Hello "));
    }
}

 4、结果如图

三、参考

1、概念流程

SpringBoot如何自定义Starter_spring boot 自定义starter-CSDN博客

2、基础 到 进阶

Spring Boot之自定义Starter_springboot自定义starter-CSDN博客

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值