Spring Boot 自定义Starter

发布于:2024-06-16 ⋅ 阅读:(17) ⋅ 点赞:(0)

自定义starter

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

定义对外接口

package org.example;

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

    String myStarterTest(String value);

}

定义对外接口的实现类

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";
    }
}

 

定义spring.factories

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

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

定义自动配置类

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();
    }

}

 安装Starter到本地仓库

mvn clean install -Dmaven.test.skip=true

 最终结果

 

二、使用自定义的starter 

创建普通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>

 查看依赖

编写测试方法

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 "));
    }
}

 结果如图

参考

概念流程

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

基础 到 进阶

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