SpringBoot——自定义start场景启动器

需求分析:为什么要学习场景启动器?

SpringBoot要引用外部组件,只需要拿到其场景启动器的依赖,再编写一些配置文件即可。
eg:SpringBoot中要使用redis就需要引入redis的场景启动器依赖

 		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

再到配置文件中编写具体参数

spring:
  redis:
    host: 42.0.0.0
    port: 6379
    password: 123456

需求:若现在我们自己编写了一个可复用组件需要封装然后在SpringBoot中引用如何实现?自定义场景启动器。
分析自定义场景启动器做出以下总结:
在这里插入图片描述
引入一个外部组件并作用的流程课归纳为:
引入starter — xxxAutoConfiguration — 容器中放入组件 ---- 绑定xxxProperties ---- 编写配置文件项
可以看到我们日常开发中只做了两个步骤1.引入xxx-starter 2.编写配置文件 中间相关操作已经被封装好了。

2.模拟案例

2.1 搭建目录架构

新建一个空项目 b0-09-customer-start 用于编写案例
在这里插入图片描述
在该空项目中新建两个工程一个用maven构建,一个用springboot初始化程序构建。
在这里插入图片描述

2.1.1. maven项目

b0-hello-spring-boot-starter
在这里插入图片描述

2.1.2 springboot初始化项目

b0-hello-spring-boot-starter-autoconfigure
在这里插入图片描述
springboot初始化选择3.0以下的版本,不在勾选其他开发场景
在这里插入图片描述

2.2 依赖修改

注:因为主要功能配置都在 xxx-autoconfigure里面,但是提供到用户引用的是 xxx-statrt,因此需要在start的maven里面引用autoconfigure
拷贝autoconfigure
在这里插入图片描述
start中引用autoconfigure依赖
在这里插入图片描述

2.3 编写业务场景

在编写业务场景之前需要删除一些用不到的内容,删除autoconfigure程序中的部分配置(maven中1.插件,2.测试依赖,3.配置文件)
在这里插入图片描述
业务场景分析,现在有一个功能需要:输出一句hello但是输出之前我们需要给其添加前缀和后缀,前后缀由使用这个组件的用户调用。假设该功能我们经常使用,我们将其抽取。
项目整体目录结构展示:
在这里插入图片描述
编写核心输出hello业务逻辑

package com.b0.hello.service;
/**
 * 组件默认不要放在容器中
 */
public class HelloService {

    public String sayHello(String userName){
    	//此处两个属性只用于理解不是最终版本,现在的报错不用理会
        return prefix + ":" +userName +">"+suffix;
    }
}

此处前后缀由使用组件的用户给出,我们将其抽离到配置类
编写配置类 HelloProperties

package com.b0.hello.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
//绑定配置文件b0.hello下所有的配置;我们可以在配置文件中通过bo.hello.prefix=b0直接对前缀赋值
@ConfigurationProperties("b0.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;
    }
}

修改核心逻辑,在配置文件中拿到前后缀属性

/**
 * 组件默认不要放在容器中,通过后面的自动配置类加入到容器中
 */
public class HelloService {
    @Autowired
    HelloProperties helloProperties;//注入配置类
    public String sayHello(String userName){
    	//从配置文件中拿到前后缀属性
        return helloProperties.getPrefix() + ":" +userName +">"+helloProperties.getSuffix();
    }
}

编写一个自动配置类来完成组件注入

package com.b0.hello.auto;

import com.b0.hello.bean.HelloProperties;
import com.b0.hello.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration//标注该类为一个配置类
@ConditionalOnMissingBean(HelloService.class)//当容器中没有配置HelloService时文件才生效(没有我们封装的核心组件下面配置的组件才生效)
@EnableConfigurationProperties(HelloProperties.class)//开启属性文件绑定功能,HelloProperties自动跟配置文件绑定,同时将HelloProperties放到容器中
public class HelloServiceAutoConfiguration {
    @Bean
    public HelloService helloService(){
        /**
         * 流程分析:1.只要我们实例化的HelloService对象一旦放在容器中
         * 2.HelloProperties会自动注入,下方sayHello方法就能拿到前后缀属性
         */
        return new HelloService();
    }
}

autoconfigure程序中在resource目录下使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
在这里插入图片描述

# EnableAutoConfiguration的值,使得项目启动加载指定的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.b0.hello.auto.HelloServiceAutoConfiguration

2.4 项目打包

将项目打包到本地,选中clean和install然后点击启动
先打包xxx-autoconfigure再打包xxx-start,因为此处start依赖autoconfigure
两个项目打包勾选上三处再点击三角形运行按钮
在这里插入图片描述
两个项目打包完成到本地仓库如下图:
在这里插入图片描述
若出现打包失败在pom.xml中加入配置

<packaging>pom</packaging>

2.5 创建开发场景 引用 自定义场景启动器

初始化springboot项目boot-09-hello-test
在这里插入图片描述
选中3.0以下版本,勾选web开发场景
在这里插入图片描述
在开发场景的maven中引入自定的场景启动器xxx-starter
笔者starter的依赖
在这里插入图片描述
拷贝到开发环境
在这里插入图片描述
项目中编写测试controller

package com.b0.boot.controller;
import com.b0.hello.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping("hello")
    public String sayHello(){
        return helloService.sayHello("李四");
    }
}

application.properties配置文件中注入属性

b0.hello.prefix=B0
b0.hello.suffix=66666

发送请求测试
在这里插入图片描述

参考资料:B站尚硅谷

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值