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

文章目录

      • 1. 定义
      • 2. 创建
      • 3. 依赖
      • 4. 编写自动配置类
      • 5. 编写 HelloWorldService 类
      • 6. 编写 Starter 配置文件
      • 7. 打包并发布到本地仓库
      • 7. 引入 HelloWorld Starter
      • 8. 使用 HelloWorld
      • 结语

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


<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.0modelVersion>

    <groupId>com.cheney.examplegroupId>
    <artifactId>hello-world-starterartifactId>
    <version>1.0-SNAPSHOTversion>

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

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-bootartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
        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 项目打包,并发布到本地仓库,以便其他项目引入。

【Spring实战】30 创建自己的 Spring Boot HelloWorld Starter_第1张图片

7. 引入 HelloWorld Starter

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

<dependencies>
    <dependency>
        <groupId>com.cheney.examplegroupId>
        <artifactId>hello-world-starterartifactId>
        <version>1.0-SNAPSHOTversion>
    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实战】30 创建自己的 Spring Boot HelloWorld Starter_第2张图片

结语

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

你可能感兴趣的:(Spring实战,spring,boot,spring,java)