Spring Boot 自定义 starter

1.Spring Boot Starter 介绍

       既然你已经读到了这篇文章,默认你可以熟练使用 Spring Boot 来完成项目开发,并已经了解了 Spring Boot starter 的基本使用。接下来来介绍一下关于 Spring Boot starter ,最后再来自定义一个 starter 玩玩。

Ⅰ.什么是 Spring Boot starter机制

       Spring Boot 中的 starter 是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath 路径下的类发现需要的 Bean,并将其注册到 IOC 容器中。Spring Boot 提供了针对日常企业应用研发各种场景的 spring-boot-starter 依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。

Ⅱ.为什么要自定义 starter

       在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个 starter,复用的时候只需要将其在 pom 中引入依赖即可,SpringBoot为我们完成自动装配,简直不要太爽。

Ⅲ.自定义starter的案例

        Spring Boot 官方已经为我们提供了足够的 starter,如下为在项目中常用到的一些

  • spring-boot-starter-web:对Web开发的支持, 包括 Tomcat 和 Spring-Mvc
  • spring-boot-starter-thymeleaf:对Thymeleaf模板引擎的支持,包括和Spring的集成
  • spring-boot-starter-aop:对面向切面编程的支持,包括 Spring-aop 和 AspectJ
  • spring-boot-starter-jdbc:对JDBC数据库的支持
  • spring-boot-starter-velocity:对 Velocity 模板引擎的支持
  • spring-boot-starter-redis:对Redis数据存储的支持,包括Spring-redis
  • 了解更多,请移步官方文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter
  • 三方starter:mybatis-spring-boot-starter:对 MyBatis 框架的支持
  • 三方starter:druid-spring-boot-starter:对 druid 数据源的支持
Ⅳ.starter 命名规则

  官方命名空间:

  • 前缀:“spring-boot-starter-”
  • 模式:spring-boot-starter-模块名
  • 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc

  自定义命名空间:

  • 后缀:"-spring-boot-starter"
  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter、druid-spring-boot-starter

2.自定义 starter 玩玩

       starter,又叫启动器。启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库。启动器主要包括两个模块:xxxx-spring-boot-starter(启动器模块)xxxx-spring-boot-autoconfigurer(自动配置模块),接下来我们就来搞一个 starter 。切记:是 jar 包格式

附:目录结构

在这里插入图片描述

Ⅰ.创建一个空项目

在这里插入图片描述

Ⅱ.创建 starter 和 autoconfigurer (Maven)模块

在这里插入图片描述

Ⅲ.starter启动器添加自动配置 autoconfigurer 依赖
<!--starter启动器-->
<dependencies>
    <!--引入starter启动器自动配置模块-->
    <dependency>
        <groupId>com.mytest</groupId>
        <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
Ⅳ.autoconfigurer 添加 spring-boot-starter 依赖
<dependencies>
	<!--引入 spring-boot-starter,这是所有starter启动器的autoconfigurer模块都需要引入的基本配置-->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
</dependencies>
Ⅴ.编写一个需要自动配置的类

       自动配置的类,相当于一个服务。在 Spring Boot 项目启动时,它会帮我们自动完成Bean 注册到 IOC 容器操作。我们在另一项目中只需要引入 starter 启动器 maven 依赖, Spring Boot 启动时帮我们自动注册到IOC容器,我们不需要任何操作便直接使用该服务。

       此处我们定义一个 HelloService 类,该类中提供一个 sayHello() 方法。为了满足不同项目参数的不同,此处我们再来定义一个HelloProperties类来设置自定义参数,设置了这个类,我们便能够通过 yml/properties 配置文件的方式来自定义设置参数了。(HelloProperties 类继续往下看,见Ⅵ)

package com.mytest.starter;
/**
 * 需要自动配置的类
 */
public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix() + "-" + name + "-" +helloProperties.getSuffix();
    }
}
Ⅵ.编写 HelloProperties 类,完成自定义参数配置
package com.mytest.starter;

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

/**
 * 设置读取配置文件前缀为 demo.hello
 * 我们可以通过 demo.hello.prefix / demo.hello.suffix 的方式来自定义
 */
@ConfigurationProperties(prefix = "demo.hello")
public class HelloProperties {

    private String prefix;

    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
Ⅶ.编写自动配置类 HelloServiceAutoConfiguration
package com.mytest.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration//标记该类是一个配置类
@ConditionalOnWebApplication//标记该自动配置类只有在 Web应用下才会生效
@EnableConfigurationProperties(HelloProperties.class)//开启自动属性配置
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean//使用@Bean注解将该Bean注册到IOC容器中
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        //通过setHelloProperties()的方式来配置自定义属性值
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
Ⅷ.重要配置,编写spring.factories文件

       在 classpath 目录下,新建 META-INF 文件夹,然后创建 spring.factories文件,将自动配置类的类名写入到 spring.factories文件中。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mytest.starter.HelloServiceAutoConfiguration

至此,配置全部完成

Ⅸ.打包

       打包(jar 包),方便其他项目引入。公司项目的话,你也可以打 jar 后,上传到公司私服,这样更方便项目的开发。本例 Demo 打包,以本地 Maven 仓库的方式进行。打包时,先打 xxx-autoconfigurer 包,再打 xxx-starter 包。因为xxx-starter 依赖 xxx-autoconfigurer 包
在这里插入图片描述

Ⅹ.其他项目使用
1.引入自定义starter Maven 依赖
<dependency>
    <groupId>com.mytest</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
2.编写一个Controller,将自定义的 HelloService 模块引入
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("hello")
    public String hello(){
        return helloService.sayHello("Spring Boot");
    }
}
3.在配置文件中进行配置
demo.hello.prefix=我是配置的前缀
demo.hello.suffix=我是配置的后缀
4.启动项目,调用 hello 接口

       项目启动正常,端口为8080,我们调用 http://localhost:8080/hello 接口,调用结果如下图所示,说明我们自定义的 starter 已经OK,没有任何问题。
在这里插入图片描述
       在以后项目日常开发工作中,独立于业务之外的配置模块,我们便可以通过自定义starter 的方式,来实现多项目共用。只需要引入一个 starter 启动类即可。

Ⅺ.附本教程Demo

     Spring Boot 自定义starter启动器 demo


博主写作不易,来个关注呗

求关注、求点赞,加个关注不迷路 ヾ(◍°∇°◍)ノ゙

博主不能保证写的所有知识点都正确,但是能保证纯手敲,错误也请指出,望轻喷 Thanks♪(・ω・)ノ

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扛麻袋的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值