周振林 周振林
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • Spring
  • SpringMVC
  • Mybatis
  • 安装教程
  • 其他教程
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏

周振林

IT界的小学生
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • Spring
  • SpringMVC
  • Mybatis
  • 安装教程
  • 其他教程
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • Spring

    • 接口设计规范
    • Spring IoC
    • Spring AOP
    • SpringBoot基础
    • SpringBoot 常用注解
    • SpringBoot Conditional注解
    • SpringBoot 组件注入方式
    • SpringBoot 组件Bean生命周期
    • SpringBoot自动配置原理
    • SpringBoot自定义Starter
      • SpringBoot其他功能
      • SpringBoot JdbcTemplate
      • SpringBoot事务
      • SpringBoot文档
      • SpringBoot Starter和BOM区别
      • SpringBoot集成Modbus实现设备
      • 查询优化N+1
      • Response设置响应编码
      • Thymeleaf教程
      • Maven教程
      • Tree工具类,轻松搞定树结构
      • 项目代码组织方式
    • SpringMVC

    • Mybatis

    • 安装教程

    • 其他教程

    • 后端
    • Spring
    周振林
    2025-09-17
    目录

    SpringBoot自定义Starter

    # 实现效果

    场景:抽取聊天机器人场景,它可以打招呼。 效果:任何项目导入此starter都具有打招呼功能,并且问候语中的人名需要可以在配置文件中修改

    # 封装项目

    1. 创建自定义starter项目robot-spring-boot-starter,引入spring-boot-starter基础依赖
    2. 编写模块功能,引入模块所有需要的依赖。
    3. 编写xxxAutoConfiguration自动配置类,帮其他项目导入这个模块需要的所有组件
    4. 在项目中导入这个start即可

    pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    RobotController类

    @RestController
    public class RobotController {
    
        @Autowired
        RobotService robotService;
    
        @RequestMapping("/robot/hello")
        public String sayHello(){
            return robotService.sayHello();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    RobotService类

    public interface RobotService {
    
        public String sayHello();
    }
    
    1
    2
    3
    4

    RobotServiceImpl类

    @Service
    public class RobotServiceImpl implements RobotService {
    
    
        @Autowired
        RobotProperties robotProperties;
    
        @Override
        public String sayHello() {
            return  "我是机器人【"+robotProperties.getName()+"】,使用底层大模型:【"+robotProperties.getModel()+"】;我们开始聊天吧";
    
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    RobotProperties类

    @Data
    @Component
    @ConfigurationProperties(prefix = "robot")  // 绑定前缀为 robot 的配置
    public class RobotProperties {
    
        private String name;
        private String model;
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    RobotAutoConfiguration类

    @Configuration
    @EnableConfigurationProperties(RobotProperties.class)
    public class RobotAutoConfiguration {
    
        @Bean
        public RobotController robotController(){
            return new RobotController();
        }
    
        @Bean
        public RobotService robotService(){
            return new RobotServiceImpl();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

    # 项目引用

    其他项目引用封装好Start的功能有三种方式

    1. @Import(RobotAutoConfiguration.class) 导入封装项目的自动配置类

    编写一个自动配置类,别人导入我的starter,无需关心需要给容器中导入哪些组件,只需要导入自动配置类,自动配置类帮你给容器中导入所有这个场景要用的组件

    1. 注解方式。只需要标注功能开关注解@EnableRobot 使用注解方式启用
    2. 只需要导入starter,所有功能就绪。

    配置文件方式 resources目录下新建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件配置Robot模块的自动配置类 (推荐)

    # @Import

    @Import(RobotAutoConfiguration.class)
    @SpringBootApplication
    public class SpringbootHelloApplication {
        public static void main(String[] args) {
        }
    }
    
    1
    2
    3
    4
    5
    6

    # 注解方式

    配置注解

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(RobotAutoConfiguration.class)
    public @interface EnableRobot {
    
    }
    
    1
    2
    3
    4
    5
    6
    7

    启动开关注解

    @EnableRobot
    @SpringBootApplication
    public class SpringbootHelloApplication {
        public static void main(String[] args) {
        }
    }
    
    1
    2
    3
    4
    5
    6

    # 配置文件方式 (推荐)

    resources目录下新建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件配置Robot模块的自动配置类

    com.zhou.robot.start.config.RobotAutoConfiguration
    
    1
    Last Updated: 2025/11/21, 16:34:23
    SpringBoot自动配置原理
    SpringBoot其他功能

    ← SpringBoot自动配置原理 SpringBoot其他功能→

    最近更新
    01
    查询优化N+1
    12-02
    02
    项目代码组织方式
    12-02
    03
    Mybatis分页插件
    12-02
    更多文章>
    Copyright © 2019-2025 鲁ICP备19032096号-1
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×