【Spring实战】30 创建自己的 Spring Boot HelloWorld Starter

Spring Boot Starter 是一项强大的功能,可以帮助我们轻松集成和配置各种常用组件。在本文中,我们将介绍如何创建一个简单的 Spring Boot HelloWorld Starter,以便于在项目中快速引入 “Hello, World!” 功能。

1. 定义

Spring Boot Starter 是一种用于简化依赖管理和配置的机制,它通过封装一组常用的依赖和默认的配置,使得开发者可以通过引入一个 Starter 来轻松地集成某个功能或组件。

2. 创建

首先,我们需要创建一个 Maven 项目,作为我们的 HelloWorld Starter 项目。项目的结构可以参考下面的示例:

hello-world-starter
|-- src
|   `-- main
|       |-- java
|       |   `-- com
|       |       `-- cheney
|       |           `-- example
|       |               `-- service
|       |                   `-- HelloWorldService.java
|       |               `-- HelloWorldAutoConfiguration.java
|       `-- resources
|           `-- META-INF
|               `-- spring.factories
|-- pom.xml

3. 依赖

添加需要的依赖,此处仅添加了 spring-boot 和 spring-boot-autoconfigure

<?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.cheney.example</groupId>
    <artifactId>hello-world-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

4. 编写自动配置类

HelloWorldAutoConfiguration.java 文件中,我们将编写一个自动配置类,用于配置 “Hello, World!” 功能。这个类需要使用 @Configuration@ConditionalOnClass 等注解。

package com.example.helloworld;

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

@Configuration
@ConditionalOnClass(HelloWorldService.class)
public class HelloWorldAutoConfiguration {

    @Bean
    public HelloWorldService helloWorldService() {
        return new HelloWorldService();
    }
}

5. 编写 HelloWorldService 类

HelloWorldService是一个简单的服务类,负责返回“Hello, World!”字符串。

package com.cheney.example.service;

import org.springframework.stereotype.Service;

@Service
public class HelloWorldService {

    public String getHelloWorld() {
        return "Hello, World!";
    }
}

6. 编写 Starter 配置文件

src/main/resources/META-INF 目录下创建一个 spring.factories 文件,用于指定自动配置类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.helloworld.HelloWorldAutoConfiguration

7. 打包并发布到本地仓库

使用 Maven 将HelloWorld Starter 项目打包,并发布到本地仓库,以便其他项目引入。

在这里插入图片描述

7. 引入 HelloWorld Starter

在其他 Spring Boot 项目的 pom.xml 文件中添加依赖:

<dependencies>
    <dependency>
        <groupId>com.cheney.example</groupId>
        <artifactId>hello-world-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

8. 使用 HelloWorld

在 Spring Boot 项目中,我们可以直接注入 HelloWorldService 并使用其中的功能。

package com.cheney.example;

import com.cheney.example.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
    @Autowired
    private HelloWorldService helloWorldService;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(helloWorldService.getHelloWorld());
    }
}

运行结果:

在这里插入图片描述

结语

通过创建自己的 Spring Boot HelloWorld Starter,我们可以更好地理解 Spring Boot Starter 的原理和用法。这是一个简单而有趣的示例,实际项目中,可以根据需求创建更复杂、更实用的 Starter,以提高代码的可维护性和重用性。希望本文对你理解和使用 Spring Boot Starter 有所帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值