Spring Boot 自定义Starter

自定义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博客 

相关推荐

  1. SpringBoot--定义starter

    2024-06-16 11:40:05       49 阅读
  2. springboot定义starter

    2024-06-16 11:40:05       44 阅读
  3. SpringBoot定义Starter

    2024-06-16 11:40:05       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 11:40:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 11:40:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 11:40:05       82 阅读
  4. Python语言-面向对象

    2024-06-16 11:40:05       91 阅读

热门阅读

  1. 521. 最长特殊序列 Ⅰ Easy

    2024-06-16 11:40:05       33 阅读
  2. centos7.9安装openssl1.1.1

    2024-06-16 11:40:05       27 阅读
  3. React项目配置路径别名“@”

    2024-06-16 11:40:05       32 阅读
  4. IO密集型任务、计算密集型任务,以及Gil锁机制

    2024-06-16 11:40:05       31 阅读
  5. 【LeetCode 383】 赎金信

    2024-06-16 11:40:05       31 阅读
  6. 民法通则配套规定(二)

    2024-06-16 11:40:05       36 阅读
  7. ChatTTS开源项目推荐

    2024-06-16 11:40:05       30 阅读