• 【Spring Boot】自定义启动器starter


    一、背景

    虽然Spring官方给我们提供了很多的启动器供我们使用
    但有时候我们也会遇到某些特殊场景,这些启动器满足不了
    这个时候就需要自定义一个启动器供我们使用

    二、自定义启动器

    在之前学习Spring Boot的过程中,我们已经对启动器有了一个大致的了解

    Spring Boot实现某个功能,一般是引入对应场景的启动器(一般不写代码,只是声明这个启动器需要引用哪些依赖),然后这个启动器又有对应的自动配置包

    1、创建一个启动器的自动配置模块

    先写和配置文件中配置项互相绑定的实体类

    package com.decade.autoConfigure.pojo;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    // 将这个类的属性和系统配置文件中的属性进行绑定
    @ConfigurationProperties(prefix = "team")
    public class TeamInfo {
        private String winnerName;
    
        private String loserName;
    
        public String getWinnerName() {
            return winnerName;
        }
    
        public void setWinnerName(String winnerName) {
            this.winnerName = winnerName;
        }
    
        public String getLoserName() {
            return loserName;
        }
    
        public void setLoserName(String loserName) {
            this.loserName = loserName;
        }
    }
    
    • 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

    再写自己要实现的业务逻辑

    package com.decade.autoConfigure.service;
    
    import com.decade.autofigure.pojo.TeamInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class TestService {
    
    	// 引入和yaml文件中配置项绑定的类
        @Autowired
        private TeamInfo teamInfo;
    
        public String testMethod() {
            return teamInfo.getWinnerName() + "今天早上,绝杀了" + teamInfo.getLoserName();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    接着,写一个自动配置类,向容器中放入组件

    package com.decade.autoConfigure.auto;
    
    import com.decade.autofigure.pojo.TeamInfo;
    import com.decade.autofigure.service.TestService;
    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;
    
    // 使得TeamInfo中的@ConfigurationProperties注解生效,将TeamInfo注册到容器中
    @EnableConfigurationProperties(TeamInfo.class)
    @Configuration
    public class AutoConfiguration {
    
        @Bean
        // 只有容器中不存在TestService这个类型的bean时,才回去初始化这个bean
        @ConditionalOnMissingBean(TestService.class)
        public TestService testService() {
            TestService testService = new TestService();
            return testService;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    最后,在新版本的Spring Boot中为了确保框架启动时加载该配置类
    我们需要在这个模块的resource文件下
    新建/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
    然后在里面写自己之前创建的自动配置类

    注意:老版可能是放在META-INF/spring.factories文件中

    com.decade.autoConfigure.auto.AutoConfiguration
    
    • 1

    2、创建一个启动器模块

    不用写任何业务代码,只需要在pom文件中,引用之前的创建的自动配置模块
    在这里插入图片描述

    3、在业务模块中引入启动器

    如图,引入我们自己定义的启动器
    在这里插入图片描述
    然后在yaml文件中设置好绑定的配置项

    再写一个测试方法进行测试即可

    package com.decade;
    
    import com.decade.autoConfigure.service.TestService;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    @Slf4j
    class SpringbootWebApplicationTests {
    
        @Autowired
        private TestService testService;
    
        @Test
        public void testCustomStarter() {
            System.out.println(testService.testMethod());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试方法结果如下图
    在这里插入图片描述

    如有错误,欢迎指正!!!

  • 相关阅读:
    【Java并发】聊聊如何优雅的暂停一个线程以及LockSupport
    java计算机毕业设计人口老龄化常态下的社区老年人管理与服务平台源程序+mysql+系统+lw文档+远程调试
    taro超过3行隐藏显示展开功能
    java集合题库详解
    《大数据之路:阿里巴巴大数据实践》-第1篇 数据技术篇 -第6章 数据服务
    CC1310F128RSMR Sub-1GHz超低功耗无线微控制器芯片
    MySQL8基于GTID以及VIP实现高可用主从架构
    [论文笔记]GPT-2
    T8161B T8403 T8448 ICS TRIPLEX 具有支持物联网边缘的计算机视觉
    【ACWing】1524. 最长回文子串
  • 原文地址:https://blog.csdn.net/Decade0712/article/details/127822532