SpringBoot自定义starter

本文详细介绍如何创建自定义Spring Boot Starter,包括模块划分、自动配置实现、属性配置及依赖管理等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析

自定义 starter 分为两个模块:

  • starter 启动器模块,空的 jar 模块,pom 引入自动配置模块。别人引用 starter 时只需引入 starter启动器模块即可
  • 自动配置模块

命名规范:
    后缀:-spring-boot-starter
    范例:mybatis-spring-boot-starter

自定义 starter 分析(自动配置模块):

  1. 我们的 starter 需要什么依赖
  2. 是否需要属性配置
  3. 配置类中需要提供哪些 Bean 交给 Spring 容器管理
  4. starter 加载是否依赖某些条件
  5. 提供 META-INF/spring.factories 文件供@EnableAutoConfiguration扫描

demo

  1. maven 项目
  2. 创建 mytest-spring-boot-starter 启动器模块,选择 jar
  3. 创建 mytest-spring-boot-starter-autoconfigure 自动配置模块,选择 jar
  4. mytest-spring-boot-starterpom 中引入 mytest-spring-boot-starter-autoconfigure

    <dependencies>
        <dependency>
            <groupId>spring-boot</groupId>
            <artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
  5. 配置mytest-spring-boot-starter-autoconfigure的pom文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
    </dependencies>
  6. mytest-spring-boot-starter-autoconfigure代码

    // 属性配置,这个配置在真正的项目中,需要在 application.yml 中配置 p7.hello 的配置项
    @ConfigurationProperties(prefix = "p7.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;
        }
    }
    
    // 我们要让别人使用这个类
    public class HelloService {
        private HelloProperties helloProperties;
        public String sayHello(String name) {
            return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
        }
        public HelloProperties getHelloProperties() {
            return helloProperties;
        }
        public void setHelloProperties(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
    }
    // 配置类
    @Configuration
    // 在 Web 环境下才被加载
    @ConditionalOnWebApplication
    // 使配置属性立即生效
    @EnableConfigurationProperties(value = HelloProperties.class)
    public class HelloServiceAutoConfiguration {
        @Autowired
        private HelloProperties helloProperties;
        @Bean
        public HelloService helloService() {
            HelloService service = new HelloService();
            service.setHelloProperties(helloProperties);
            return service;
        }
    }
    
    // 在 src/main/resources 下创建 META-INF 文件夹,再创建 spring.factories 文件,配置我们的配置类
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.p7.autoconfigure.HelloServiceAutoConfiguration
  7. 创建 Springbootweb 工程,在 pom 中引入 starter 启动器

    <dependency>
        <groupId>spring-boot</groupId>
        <artifactId>mytest-spring-boot-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  8. application.yml 中配置 starter 中需要的属性

    p7:
      hello:
        prefix: p7
        suffix: hello
  9. 创建测试 Controller

    @RestController("/hello")
    public class HelloController {
        @Autowired
        private HelloService helloService;
        @GetMapping
        public String hello(){
            return helloService.sayHello("say");
        }
    }
  10. 启动 web 项目,访问 localhost:8080/hello

    // web 启动类
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }

    这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值