zoukankan      html  css  js  c++  java
  • SpringBoot06:自定义starter

    自定义Starter

    我们分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩!

    说明

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

    命名归约:

    官方命名:

    • 前缀:spring-boot-starter-xxx

    • 比如:spring-boot-starter-web…

    自定义命名:

    • xxx-spring-boot-starter

    • 比如:mybatis-spring-boot-starter

    编写启动器

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

    2、新建一个普通Maven模块:kuang-spring-boot-starter

    在这里插入图片描述

    3、新建一个Springboot模块:kuang-spring-boot-starter-autoconfigure

    在这里插入图片描述

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

    在这里插入图片描述

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

    <!-- 启动器 -->
    <dependencies>
        <!--  引入自动配置模块 -->
        <dependency>
            <groupId>com.kuang</groupId>
            <artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    

    6、将 autoconfigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置!

    在这里插入图片描述

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

    package com.kuang;
     
     
    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.kuang;
     
     
    import org.springframework.boot.context.properties.ConfigurationProperties;
     
     
    // 前缀 kuang.hello
    @ConfigurationProperties(prefix = "kuang.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.kuang;
     
     
    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.kuang.HelloServiceAutoConfiguration
    

    11、编写完成后,可以安装到maven仓库中!

    在这里插入图片描述

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

    1、新建一个SpringBoot 项目

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

    <dependency>
        <groupId>com.kuang</groupId>
        <artifactId>kuang-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    

    3、编写一个 HelloController 进行测试我们自己的写的接口!

    package com.kuang.controller;
     
     
    @RestController
    public class HelloController {
     
     
        @Autowired
        HelloService helloService;
     
     
        @RequestMapping("/hello")
        public String hello(){
            return helloService.sayHello("zxc");
        }
     
     
    }
    

    4、编写配置文件 application.properties

    kuang.hello.prefix="ppp"
    kuang.hello.suffix="sss"
    

    5、启动项目进行测试,结果成功 !
    在这里插入图片描述

    ————————————————

    原文链接:https://blog.csdn.net/qq_33369905/article/details/106647278

    作者:little-cheap

    -------------------------------------------

    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!

  • 相关阅读:
    基本二叉搜索树的第K小元素
    sklearn常见分类器(二分类模板)
    python图论包networks(最短路,最小生成树带包)
    PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
    PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
    Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题。 username/
    JavaScritpt的DOM初探之Node(一)
    怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)
    Ubuntu 14.04下单节点Ceph安装(by quqi99)
    卡片游戏
  • 原文地址:https://www.cnblogs.com/little-cheap/p/15317866.html
Copyright © 2011-2022 走看看