• 实现自定义Spring Boot Starter



    Spring Boot 可以省略众多的繁琐配置,它的众多 starter 可以说是功不可没。 Spring Boot默认定义了很多的starter(spring-boot-starter-xxx),这些starter的简化了很多烦琐的配置,通过引入各种 Spring Boot Starter 包可以快速搭建出一个项目的脚手架。常用的 starter 还有 spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-jdbc,相比于传统的 xml 配置可以说是大大减少了集成的工作量。不过也是不可能应付所有需求的,根据我们自己的业务,我们可以自定义 starter 来实现项目中复用度高的业务,让别的模块能很方便的引入使用。

    一、原理

    利用 starter 实现自动化配置只需要两个条件:maven 依赖和配置文件,这里简单介绍下starter 实现自动化配置的流程。
    引入 maven 依赖实质上就是导入 jar 包,Spring Boot 项目启动的时候会找到 starter jar 包中的 resources/META-INF/spring.factories 文件,根据 spring.factories 文件中的配置,加载需要自动配置的类。
    以 mybatis-spring-boot-starter 为例,其自动配置类之一如下:

    @Configuration
    @ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
    @ConditionalOnSingleCandidate(DataSource.class)
    @EnableConfigurationProperties({MybatisProperties.class})
    @AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})
    public class MybatisAutoConfiguration implements InitializingBean {
        ...
    }        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    对 MybatisAutoConfiguration 中用到的注解的说明,参考如何实现自定义的 Spring Boot Starter
    在这里插入图片描述
    mybatis-spring-boot-starter 的 pom 文件引用了 mybatis-spring-boot-autoconfigure 依赖,其中的 spring.factories 文件配置如下:

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration,\
    org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这样,便可以在 Spring Boot 项目启动的时候会加载 mybatis starter 中的 MybatisLanguageDriverAutoConfiguration 和 MybatisAutoConfiguration 类。

    二、实战

    首先实现自定义的 spring-boot-starter;其次,通过一个新的工程 来引入该 starter,测试自定义starter是否生效。

    1 自定义 Spring Boot Starter

    功能描述:此starter供使用时获取到配置文件的属性值,作为业务逻辑的参数实现相关功能。

    1.1 添加maven依赖

    这里说下artifactId的命名问题:Spring 官方 Starter 通常命名为 spring-boot-starter-{name},如 spring-boot-starter-web;非官方 Starter 命名应遵循{name}-spring-boot-starter的格式

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-autoconfigureartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    1.2 属性类AuthorProperties

    用于获取配置文件前缀为custom的value值

    @ConfigurationProperties(prefix = "custom")
    public class AuthorProperties {
        public static final String DEFAULT_AUTHOR = "zsh";
        public String author = DEFAULT_AUTHOR;
        public String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.3 自动配置类AuthorAutoConfiguration

    @Configuration
    @ConditionalOnClass({ AuthorServer.class })
    @EnableConfigurationProperties(AuthorProperties.class)
    public class AuthorAutoConfiguration {
        @Resource
        private AuthorProperties authorProperties;
        @Bean
        @ConditionalOnMissingBean(AuthorServer.class)
        @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
        public AuthorServer authorResolver() {
            AuthorServer authorServer = new AuthorServer();
            authorServer.setAuthor(authorProperties.getAuthor());
            return authorServer;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    @ConditionalOnClass 表示有 ExampleService 类存在时执行。
    @EnableConfigurationProperties 表示有 ExampleProperties 类存在时执行。
    @ConditionalOnMissingBean 当 ExampleService Bean 缺失时,执行下段代码。
    @ConditionalOnProperty 当 example.service.enabled = true 时,执行代码。

    1.4 业务逻辑AuthorServer

    public class AuthorServer {
        public String author;
        public String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.5 spring.factories

    在 resources 目录下新建 META_INF 文件夹,并在其中新建 spring.factories,在其中写入:

    # CUSTOM
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.zsh.AuthorAutoConfiguration
    
    • 1
    • 2
    • 3

    至此,Starter 项目创建完成,使用 mvn install 命令打包。
    在这里插入图片描述
    出现BUILD SUCCESS代表打包成功!
    在这里插入图片描述

    2 测试自定义的 Spring Boot Starter

    2.1 新建module或者新建工程

    在pom文件中引入自定义的Spring Boot Starter

    <dependency>
    			<groupId>com.zsh</groupId>
    			<artifactId>zsh-spring-boot-starter</artifactId>
    			<version>1.0-SNAPSHOT</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2 创建controller

    @RestController
    @RequestMapping("/userController")
    public class UserController {
    
    	@Autowired
    	private UserService userService;
    
    
    	@Autowired
    	private AuthorServer authorServer;
    
    	//获取自定义stater方法
    	@RequestMapping("/author")
    	String home() {
    		return "发布者:"+ authorServer.getAuthor();
    	}
    }	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.3 yml配置文件

    custom:
      author: hello-world
      enabled: true
    
    • 1
    • 2
    • 3

    2.4 启动项目

    http请求结果如下,代表新工程调用自定义starter成功!

    localhost:8086/userController/author
    
    • 1

    在这里插入图片描述

    2.5 项目整体结构及源码

    源码下载,欢迎star!SpringBoot2.XLearn
    在这里插入图片描述
    参考资料
    springboot自定义和使用starter
    【企业级解决方案一】编写一个自己的SpringBootStarter
    springboot如何自定义starter
    如何实现自定义的 Spring Boot Starter?

  • 相关阅读:
    SMT贴片制造:专业、现代、智能的未来之选
    【21天python打卡】第16天 python经典案例(2)
    (1)(1.16) Maxbotix I2C声纳
    Chrome 扩展教程之如何使用 React 构建 Chrome 扩展(教程含源码)
    48-安装软件并管理服务
    电容的分类
    [山东科技大学OJ]1215 Problem C: 编写函数:字符串的复制 之一 (Append Code)
    Python 中的分步机器学习
    美妆行业全网声量统计与传播趋势分析,完美日记位居品牌声量榜一
    Element-UI 快速入门指南
  • 原文地址:https://blog.csdn.net/jike11231/article/details/128159308