✏️
blog
  • README
  • 2023 11
    • expect使用
  • 2023 10
    • 通过Appium给iOS应用自动化执行脚本
  • 2023 06
    • 三种ThreadLocal详解
    • 常见限流算法总结
    • 分布式ID生成算法
  • 2023 05
    • 线上机器CLOSE_WAIT连接数异常排查
    • 多数据源引发transactional事务回滚失效
  • 2023 04
    • MySQL中BufferPool
  • 2022 12
    • Linux IO
    • Netty总结
  • 2022 04
    • Thrift
  • 2022 03
    • JVM命令总结
    • 频繁FullGC定位思路
    • Redis总结
    • Spring常见问题总结
    • Kafka总结
  • 2022 02
    • Dubbo柔性服务天池大赛总结
  • 2021 12
    • 泛型中的extends和super
    • 手写一个Spring Boot Starter
  • 2021 11
    • 常用消息队列总结
  • 2021 10
    • swagger2快速使用
    • SpringBoot接口cors跨域访问
  • 2021 08
    • 常用shell命令总结
  • 2021 05
    • 线程cpu飙升排查
    • zookeeper install
  • 2021 04
    • Java虚拟机
    • [Spring Boot](2021-04/2021-04-04-Spring Boot.md)
    • [Spring MVC](2021-04/2021-04-04-Spring MVC.md)
    • 分布式ID
    • 消息队列
    • [Spring AOP](2021-04/2021-04-05-Spring AOP.md)
    • 布隆过滤器
    • Scala内核Spark阻塞排查
  • 2020 12
    • 使用Python优雅实现tail命令
  • 2020 11
    • Spark基础架构
    • 一文搞定Git
    • Spark线上问题引发的思考
  • 2020 04
    • 使用GitBook
  • 2019 05
    • SELinux、Netfilter、iptables、firewall和ufw五者关系
    • 安装npm和nodejs
    • 访问不到云服务器中的项目
  • 2019 04
    • 二叉树中节点与度数
    • 实现会话跟踪的技术有哪些
    • 计算机操作系统-死锁
    • Semaphore Count Down Latch Cyclic Barrier
    • Java内存模型
    • 双重检查锁定
    • synchronized实现底层
    • Lock接口
    • HTTP与HTTPS的区别
    • Java中线程池
    • Java中的阻塞队列
    • 排序算法
  • 2019 03
    • MySQL中索引
    • MySQL存储引擎
    • MySQL锁机制
    • n的阶乘结果后面0的个数
由 GitBook 提供支持
在本页

这有帮助吗?

  1. 2021 12

手写一个Spring Boot Starter

上一页泛型中的extends和super下一页2021 11

最后更新于3年前

这有帮助吗?


  1. 创建一个maven项目 hello-spring-boot-starter

  2. 添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
  3. 创建 HelloProperties, HelloService, HelloAutoConfiguration

    // HelloProperties.java
    // ConfigurationProperties只有在EnableConfigurationProperties显示开启时才生效
    
    @ConfigurationProperties(prefix = "hello")
    public class HelloProperties {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    // HelloService的实现类 HelloServiceImpl.java
    
    public class HelloServiceImpl implements HelloService {
    
        @Resource
        private HelloProperties helloProperties;
    
        public String sayHello() {
            System.out.println("hello, " + helloProperties.getName());
            return "";
        }
    } 
    // HelloAutoConfiguration.java
    
    @Configuration
    @ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
    @EnableConfigurationProperties(HelloProperties.class)
    @ConditionalOnClass(HelloService.class)
    public class HelloAutoConfiguration {
    
        @Bean
        @ConditionalOnMissingBean(HelloService.class)
        public HelloService helloService() {
            return new HelloServiceImpl();
        }
    } 
  4. 在resources目录下添加 spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.HelloAutoConfiguration
  5. 整体目录结构如下,已完成基本的开发工作

    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── org
    │   │   │       └── example
    │   │   │           ├── HelloAutoConfiguration.java
    │   │   │           ├── HelloProperties.java
    │   │   │           └── service
    │   │   │               ├── HelloService.java
    │   │   │               └── impl
    │   │   │                   └── HelloServiceImpl.java
    │   │   └── resources
    │   │       └── META-INF
    │   │           └── spring.factories
  6. mvn install 安装到本地maven仓库进行测试

  7. spring-boot-web 测试模块引入依赖

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>hello-spring-boot-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
官方文档
完整代码GitHub