手把手创建Springboot的Starter

本文介绍了如何构建一个Spring Boot Starter,包括新建Maven工程、定义接口和实现、配置类以及在Spring的META-INF/spring.factories文件中声明配置。完成后,使用者只需引入依赖即可直接使用模块接口。示例展示了在Web工程中引入Starter并创建Controller调用接口的方法,实现了便捷的代码复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1. Starter的构建

  • 第一步:新建Maven工程,pom文件引入Springboot的依赖包
<?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>

    <groupId>com.springstudy</groupId>
    <artifactId>study-client-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>
  • 第二步:定义一个接口和简单的实现
package com.spring.study.module.impl;

import com.spring.study.module.HelloService;
import org.springframework.stereotype.Component;

/**
 * @author HunterQiu
 * @version 1.0
 * @date 2021/06/09 0:21
 * @description
 */

@Component
public class HelloServiceImpl implements HelloService {
    public String sayHello() {
        return "Hello Yihang";
    }
}
  • 第三步:新建一个配置类,逻辑实现可以为空,重要的是注解
package com.spring.study.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author HunterQiu
 * @version 1.0
 * @date 2021/06/09 0:23
 * @description
 */
@Configuration
@ComponentScan({"com.spring.study.module"})
public class HelloServiceAutoConfiguration {
}

  • 第四步:在Spring的根目录,也就是resources目录下建立META-INF/spring.factories文件,并声明配置文件的路径

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 
com.spring.study.config.HelloServiceAutoConfiguration

至此,一个标准的Starter开发完成。

2. Starter的使用

2.1 在你的Web工程pom里面,加入starter的依赖

<dependency>
    <groupId>com.springstudy</groupId>
    <artifactId>study-client-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2.2 新建一个controller,将模块的逻辑引入,如下所示

package gdufs.edu.com.controller;

import com.spring.study.module.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author HunterQiu
 * @version 1.0
 * @date 2021/06/08 23:27
 * @description
 */

@RestController
public class TestController {

    @Autowired
    private HelloService helloService;

    @RequestMapping("/")
    public String home(){
       return helloService.sayHello();
    }
}

你会发现,刚才开发的Starter对于使用者来说非常方便,除了在pom中引入依赖,什么都不做就可以直接使用模块内部的接口注入。

本地IDEA启动Springboot,到浏览器上提交请求,可见结果成功返回。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值