从01开始 从01开始
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)

peterjxl

人生如逆旅,我亦是行人
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)
  • JavaSE

  • JavaSenior

  • JavaEE

  • JavaWeb

  • Spring

  • 主流框架

  • SpringMVC

  • SpringBoot

    • SpringBoot教程-尚硅谷

      • SpringBoot课程介绍
      • Spring和SpringBoot
      • HelloWorld
      • 了解自动配置原理
      • 底层注解-@Configuration详解
      • 底层注解-@Import导入组件
      • 底层注解-@Conditional条件装配
      • 原生配置文件引入-@ImportResource
      • 底层注解-配置绑定@ConfigurationProperties
      • 自动配置原理
      • 自动配置流程
      • Lombok简化开发
      • DevTools
      • Spring-Initailizr
      • 配置文件-Yaml用法
      • Web开发简介
      • web开发-静态资源规则于定制化
      • 静态资源配置原理
      • Rest映射及源码解析
      • 请求映射原理
      • 常用参数注解使用
      • MatrixVariable:矩阵变量
      • 各种类型参数解析原理
      • Servlet-API参数解析原理
      • Model、Map参数解析原理
      • 自定义对象参数绑定原理
      • 自定义Converter原理
      • 数据响应原理
      • 内容协商原理
      • 基于请求参数的内容原理
      • 自定义MessageConverter原理
      • Thymeleaf初体验
      • web实验-后台管理系统
      • web实验-抽取公共页面
      • web实验-遍历数据
      • 源码分析-视图解析器与视图
      • 拦截器-登录检查与静态资源放行
      • 拦截器的执行时机和原理
      • 单文件和多文件上传的使用
      • 文件上传原理
      • 错误处理机制
      • 错误处理-底层组件源码分析
      • 异常处理流程
      • 几种异常处理原理
      • Web原生对象注入
      • 嵌入式Servlet容器
      • 定制化原理
      • 数据库场景的自动配置分析和整合测试
      • 自定义方式整合Druid
      • 通过starter整合Druid
      • 整合Mybatis
      • 使用注解整合Mybatis
      • 整合MybatisPlus操作数据库
      • MybatisPlus-列表分页展示
      • 整合Redis
      • 单元测试-Junit5
      • 单元测试-断言机制
      • 单元测试-前置条件
      • 单元测试-嵌套测试
      • 单元测试-参数化测试
      • 指标监控-基本概念
      • 指标监控-配置EndPoint
      • 指标监控-可视化
      • 原理解析-Profile功能
      • 配置文件深入
      • 自定义Starter
        • Starter启动原理
        • 新建项目
        • 新增组件HelloService
        • 新建测试项目
        • 源码
      • SpringApplication初始化过程
      • SpringBoot完整启动过程
      • SpringBoot
  • Java并发

  • Java源码

  • JVM

  • 韩顺平

  • Java
  • Java
  • SpringBoot
  • SpringBoot教程-尚硅谷
2023-08-22
目录

自定义Starter

# 660.自定义Starter

本文讲讲如何自定义Starter   有些功能是经常用的,我们希望可以将其抽取出来,这样其他微服务引用时,也能自动配置好,此时就得自定义Starter。

‍

‍

# Starter启动原理

先来复习下原理:

  • 首先一个Starter仅仅是描述了引入了什么依赖
  • 然后该Starter会引入一个自定义的AutoConfigure,自动配置这些依赖
  • AutoConfigure底层会引用SpringBoot官方的spring-boot-starter,该官方starter是最底层的,每个AutoConfigure都要引入

​

‍

‍

例如,spring-boot-starter-test​这个starter,仅仅描述了引用了什么依赖,几乎没什么代码;而其引入的spring-boot-test-autoconfigure​,才是完成配置的

​​​

‍

‍

# 新建项目

新建一个空项目(方便创建多个模块):

​​

‍

‍

新建一个模块​atguigu-hello-spring-boot-starter-autoconfigure​,作为AutoConfigure

​​​​

其pom.xml文件如下(多余的内容我们可以先删掉):

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

    <groupId>com.peterjxl</groupId>
    <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>atguigu-hello-spring-boot-starter-autoconfigure</name>
    <description>atguigu-hello-spring-boot-starter-autoconfigure</description>


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

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

</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

‍

‍

然后新建一个模块,作为starter:

​

​

然后我们在starter的模块中,引入autoconfigure(引入后可能会提示找不到,后续我们会install自动配置模块到本地仓库中)

<?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.peterjxl</groupId>
    <artifactId>atguigu-hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.peterjxl</groupId>
            <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

‍

‍

# 新增组件HelloService

假设我们自定义了一个组件:HelloService,这个是很有用的组件,要被其他模块引入。我们先定义一个配置类,用于HelloService的配置读取:

package com.peterjxl.atguiguhellospringbootstarterautoconfigure.bean;


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

@ConfigurationProperties(prefix = "atguigu.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;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

‍

‍

然后新建HelloService类:

package com.peterjxl.atguiguhellospringbootstarterautoconfigure.controller;

import com.peterjxl.atguiguhellospringbootstarterautoconfigure.bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 默认不要放在容器中
 */
public class HelloService {

    @Autowired
    HelloProperties helloProperties;


    public String sayHello(String name) {
        return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

‍

至于该组件是否放在容器中,我们单独写一个配置类:

package com.peterjxl.atguiguhellospringbootstarterautoconfigure.auto;


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;
import com.peterjxl.atguiguhellospringbootstarterautoconfigure.controller.HelloService;
import com.peterjxl.atguiguhellospringbootstarterautoconfigure.bean.HelloProperties;

@Configuration
@EnableConfigurationProperties(HelloProperties.class) //默认会放在容器中
public class HelloServiceAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        return helloService;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

‍

‍

‍

新建配置文件resources\META-INF\spring.factories:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.peterjxl.atguiguhellospringbootstarterautoconfigure.auto.HelloServiceAutoConfiguration \
1
2
3

‍

写完后,我们可以进行打包(mvn install),安装到本地仓库

​​

‍

‍

然后starter同理,也要mvn clean 和 mvn install,这样别的项目就可以引入该starter了。

‍

# 新建测试项目

接下来创建一个测试项目,用来引入starter并测试HelloService。可以引入web开发场景的依赖:

​​

‍

并引入starter:

<dependency>
    <groupId>com.peterjxl</groupId>
    <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
1
2
3
4
5

‍

在配置文件中application.yml中:

atguigu.hello.prefix=ATGUIGU
atguigu.hello.suffix=666
1
2

‍

然后新增一个Controller,用来注入HelloService:

package com.peterjxl.learnspringbootcustomerstartertest.controller;


import com.peterjxl.atguiguhellospringbootstarterautoconfigure.controller.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("张三");
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

‍

启动项目,可以项目运行效果一致:

​​

‍

‍

# 源码

已将本文源码上传到 Gitee (opens new window) 和 GitHub (opens new window)

  • LearnSpringBoot-Customer-Starter:包含starter和autoconfigure两个模块
  • LearnSpringBoot-Customer-Starter-Test:用于引入starter

‍

在GitHub上编辑此页 (opens new window)
上次更新: 2023/8/23 10:10:57
配置文件深入
SpringApplication初始化过程

← 配置文件深入 SpringApplication初始化过程→

Theme by Vdoing | Copyright © 2022-2023 粤ICP备2022067627号-1 粤公网安备 44011302003646号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式