自定义Springboot Starter

创建一个Springboot Starter,借助该Starter我们可以自定义欢迎消息。
本Starter的内容不是重点,重点是创建Starter的流程。

1. 创建Starter工程

1.1 创建Springboot项目

在这里插入图片描述

1.2 导入相关依赖,删除spring-boot-maven-plugin

<?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.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.wyw</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot-starter</name>

    <description>hello-spring-boot-starter</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.7.13</version>
        </dependency>

        <!--配置文件的自动提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.5.9</version>
        </dependency>
    </dependencies>

    <!--注释掉该插件-->
<!--    <build>-->
<!--        <plugins>-->
<!--            <plugin>-->
<!--                <groupId>org.springframework.boot</groupId>-->
<!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
<!--            </plugin>-->
<!--        </plugins>-->
<!--    </build>-->

</project>

1.3 创建属性类

在此类上添加@ConfigurationProperties注解,@ConfigurationProperties是Springboot提供读取配置文件的一个注解。

package com.wyw.hellospringbootstarter.property;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author name:    silk
 * @version 1.0
 * @description: 创建 HelloProperties 属性类, 在此类上添加 @ConfigurationProperties注解
 * @date 2024/6/6 21:25
 */
// prefix = "com.wyw.hello" 根据前缀去读取配置文件中配置的对应的属性信息
@ConfigurationProperties(prefix = "com.wyw.hello")
public class HelloProperties {

    // 欢迎谁
    private String name;

    // 欢迎语
    private String content;

    // 欢迎语说几遍
    private int loop;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getLoop() {
        return loop;
    }

    public void setLoop(int loop) {
        this.loop = loop;
    }
}

1.4 创建服务类,无需注入到Spring容器

package com.wyw.hellospringbootstarter.service;

/**
 * @author name:    silk
 * @version 1.0
 * @description: 创建使用属性类的方法类 XXXService, 该类无需注入到spring容器中
 * @date 2024/6/6 21:31
 */
public class HelloService {

    private String name;

    private String content;

    private int loop;

    public HelloService() {
    }

    public HelloService(String name, String content, int loop) {
        this.name = name;
        this.content = content;
        this.loop = loop;
    }

    public void sayHello() {
        for (int i = 0; i < loop; i++) {
            System.out.println("Hello " + name + " , " + content);
        }
    }
}

1.5 创建自动配置类

添加@Configuration@EnableConfigurationProperties注解,导入对应的属性类。

package com.wyw.hellospringbootstarter.config;

import com.wyw.hellospringbootstarter.property.HelloProperties;
import com.wyw.hellospringbootstarter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author name:    silk
 * @version 1.0
 * @description: 自动装配类
 * @date 2024/6/6 21:36
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {

    @Autowired
    private final HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        return new HelloService(helloProperties.getName(), helloProperties.getContent(), helloProperties.getLoop());
    }
}

1.6 META-INF/spring.factories中配置上自动装配类

org.springframework.boot.autoconfigure.EnableAutoConfiguration =\
com.wyw.hellospringbootstarter.config.HelloAutoConfiguration

1.7 Maven打包到本地仓库

maven clean
maven install

2. 项目中导入Starter,验证

2.1 导入Starter依赖

<!--导入自定义starter-->
        <dependency>
            <groupId>com.wyw</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

2.2 写配置文件

com:
  wyw:
    hello:
      name: xiaoWang	# 顾客姓名
      content: you are a member user, and our store will give you two 20 yuan discount coupons.	# 欢迎语
      loop: 3	# 欢迎语次数

2.3 注入服务类,验证

通过@AutowiredHelloService实例化。

package com.wyw.learn.diystarter;

import com.wyw.hellospringbootstarter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author name:    silk
 * @version 1.0
 * @description: TODO
 * @date 2024/6/6 21:49
 */
@RestController
@RequestMapping("/diystarter")
public class DiyHelloStarterTest {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public void sayHello() {
        helloService.sayHello();
    }
}

运行结果:

在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值