手撸一个SpringBoot场景启动器Starter实现组件复用

前言

SpringBoot能够实现快速开发,其中一点是底层为我们自动化配置好了一个个Starter,我们只需根据自身需求,修改相应配置项即可。那如何来定制化一个Starter,实现功能的复用呢?

定制化mytest-spring-boot-starter

编码之前,我们可以回顾一下SpringBoot的自动装配流程,这里我简单以导入mybatis的starter为例。

	   <!--导入的mybatis-spring-boot-starter-->
       <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
	
		<!--底层引入相关包-->
		 <dependency>
     	 	<groupId>org.mybatis.spring.boot</groupId>
      		<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
    	</dependency>
	    <dependency>
	      <groupId>org.mybatis</groupId>
	      <artifactId>mybatis</artifactId>
	    </dependency>
	    <dependency>
	      <groupId>org.mybatis</groupId>
	      <artifactId>mybatis-spring</artifactId>
	    </dependency>

我们看到引入mybatis-spring-boot-starter,底层也会响应引入mybatis-spring-boot-autoconfigure的自动配置包。点进mybatis-spring-boot-autoconfigure看到更核心的依赖:

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

    <!-- Optional dependencies -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <optional>true</optional>
    </dependency>

看到这,我们大致清楚整个依赖的关系:mybatis-spring-boot-starter->mybatis-spring-boot-autoconfigure->spring-boot-autoconfigure;所以我们如果定制化场景器的话,提供xxx-spring-boot-starter作为依赖供其他应用引入,而重点实现逻辑关注xxx-spring-boot-autoconfigure,并且底层以spring-boot-autoconfigure作为核心组件。

自定义mytest-spring-boot-startermytest-spring-boot-starter-autoconfigure
在同一个空项目下初始化两个子工程,命名分别是mytest-spring-boot-startermytest-spring-boot-starter-autoconfigure
在这里插入图片描述
mytest-spring-boot-starte工程作为外界引入包,其中POM文件引入以下依赖:

<?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.gz.xf</groupId>
    <artifactId>mytest-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!--引入自定义配置包-->
    <dependencies>
        <dependency>
            <groupId>com.gz.xf</groupId>
            <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

mytest-spring-boot-starter-autoconfigure作为自动配置包,需要提供SpringBoot自动装配功能,所以要也是要单独作为SpringBoot引用。不过POM文件只保存一下依赖:

<?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>
    <groupId>com.gz.xf</groupId>
    <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mytest-spring-boot-starter-autoconfigure</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
    	<!--SpringBoot核心Starter-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--属性配置绑定提示:可选-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
  • 需要复用的组件HelloService
/**
 * @Description
 * @Author Fangchenjiang
 * @Date 2021/10/11 9:37
 */

public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public String sayHello(String name) {
        return helloProperties.getPrefix() + ":" + name + ":" + helloProperties.getSuffix();
    }
}

在这里插入图片描述

  • 配置项绑定类
/**
 * @Description
 * @Author Fangchenjiang
 * @Date 2021/10/11 9:37
 */
@ConfigurationProperties(prefix = "hello")
class HelloProperties {

    String prefix;
    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;
    }
}
  • 自动配置类
/**
 * @Description
 * @Author Fangchenjiang
 * @Date 2021/10/11 9:43
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)  //开启属性配置绑定
public class HelloServiceAutoConfiguration {

    @ConditionalOnMissingBean(HelloService.class)  //若容器中没指定Bean才注册
    @Bean
    public HelloService helloService() {
        return new HelloService();
    }

}
  • 包扫描文件
    删掉原来SpringBoot类路径下的所有东西,创建META/spring.factories并指定开启自动配置的类路径
    在这里插入图片描述
  • 打包并安装
    在这里插入图片描述

安装到本地仓库Maven提示:

[INFO] --- maven-install-plugin:2.4:install (default-install) @ mytest-spring-boot-starter ---
[INFO] Installing E:\code\mytest-spring-boot-starter\mytest-spring-boot-starter\target\mytest-spring-boot-starter-1.0-SNAPSHOT.jar to E:\Local\maveRepository\com\gz\xf\mytest-spring-boot-starter\1.0-SNAPSHOT\mytest-spring-boot-starter-1.0-SNAPSHOT.jar
[INFO] Installing E:\code\mytest-spring-boot-starter\mytest-spring-boot-starter\pom.xml to E:\Local\maveRepository\com\gz\xf\mytest-spring-boot-starter\1.0-SNAPSHOT\mytest-spring-boot-starter-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

引入并使用

新建某个工程,引入本地定制化的Starter依赖

  		<dependency>
            <groupId>com.gz.xf</groupId>
            <artifactId>mytest-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
/**
 * @Description
 * @Author Fangchenjiang
 * @Date 2021/8/26 10:03
 */
@RestController
public class TestController {

    /**
     * 注入定制化Starter的组件
     */
    @Autowired
    HelloService helloService;
    @GetMapping("/test")
    public String test() {
    return helloService.sayHello("小方");
    }
}
server:
  port: 8888
spring:
  application:
    name: demo

#属性绑定
hello:
  suffix: 123
  prefix: 456

浏览器访问:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值