栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot-06 自定义starter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot-06 自定义starter

自定义starter

https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247483767&idx=1&sn=4c23abf553259052f335086dba1ce80c&scene=19#wechat_redirect
我们分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩

1、说明

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

命名归约:

官方命名:

  • 前缀:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web…

自定义命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter
2、编辑启动器

步骤:

  1. 在IDEA中新建一个空项目 spring-boot-starter-diy

  2. 新建一个普通 Maven 模块:aze-spring-boot-starter

  3. 新建一个 Springboot 模块:aze-spring-boot-starter-autoconfigure

  4. 点击apply即可,基本结构

  5. 在我们的 starter 中 导入 autoconfigure 的依赖!

    
        
        
            com.aze
            aze-spring-boot-starter-autoconfigure
            0.0.1-SNAPSHOT
        
    
    
  6. 将 autoconfigure 项目下多余的文件都删掉,Pom 中只留下一个 starter,这是所有的启动器基本配置

  7. 我们编写一个自己的服务

    package com.aze;
    
    public class HelloService {
    
        HelloProperties helloProperties;
    
        public HelloProperties getHelloProperties() {
            return helloProperties;
        }
    
        public void setHelloProperties(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
    
        public String sayHello(String name){
            return helloProperties.getPrefix() + name + helloProperties.getSuffix();
        }
    
    }
    
  8. 编写 HelloProperties 配置类

    package com.aze;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    // 前缀 kuang.hello
    @ConfigurationProperties(prefix = "aze.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;
        }
    }
    
  9. 编写我们的自动配置类并注入bean,测试!

    package com.aze;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ConditionalOnWebApplication //web应用生效
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloServiceAutoConfiguration {
    
        @Autowired
        HelloProperties helloProperties;
    
        @Bean
        public HelloService helloService(){
            HelloService service = new HelloService();
            service.setHelloProperties(helloProperties);
            return service;
        }
    
    }
    
  10. 在 resources 编写一个自己的 meta-INFspring.factories

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.aze.HelloServiceAutoConfiguration
    
  11. 编写完成后,可以安装到maven仓库中

3、新建项目测试我们自己写的启动器

步骤:

  1. 新建一个 SpringBoot 项目

  2. 导入我们自己写的启动器

    
        com.aze
        aze-spring-boot-starter
        1.0-SNAPSHOT
    
    
  3. 编写一个 HelloController 进行测试我们自己的写的接口!

    package com.aze.controller;
    
    import com.aze.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Autowired
        HelloService helloService;
    
        @RequestMapping("/hello")
        public String hello(){
            return helloService.sayHello("hello1");
        }
    
    }
    
  4. 编写配置文件 application.yaml

    aze:
      hello:
        prefix: '"abc"'
        suffix: '"123"'
    
  5. 启动项目进行测试,结果成功 !

4、总结

学完的东西一定要多下去实践

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号