ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot自定义Starters

2019-07-19 14:06:26  阅读:179  来源: 互联网

标签:zhangbao SpringBoot 自定义 spring 配置 boot autoconfigure Starters starter


#如何自定义Starters#

### 1.创建思路 ###
1.这个场景需要用到的依赖是什么?

2.如何编写自动配置,一些注解的功能?

    @Configuration //指定这个类是一个配置类
    @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
    @AutoConfigureAfter //指定自动配置类的顺序
    @Bean //给容器中添加组件
    @ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
    @EnableConfigurationProperties //让xxxProperties生效加入到容器中
    自动配置类要能加载
    将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

3.starter的模式:

* 启动器只是用来做依赖导入
* 自动配置模块用来写代码逻辑
* 启动器依赖自动配置,只需要引入启动器会自动引入自动配置
* 自定义启动器名:启动器名-spring-boot-starter

### 2.创建流程 ###
1.创建一个自动配置项目:zhangbao-spring-boot-starter-autoconfigure,在pom.xml文件中导入以下依赖

    <!‐‐引入spring‐boot‐starter;所有starter的基本配置‐‐>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- 使用ConfigurationProperties需要导入的依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>


2.创建一个maven项目:zhangbao-spring-boot-starter,只在pom.xml文件中导入自动配置项目的依赖

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

3.编写配置类:HelloProperties

    //data注解是lombok插件的注解用来生成属性的get/set方法
    @Data
    @ConfigurationProperties(prefix = "spring.zhangbao.hello")
    public class HelloProperties {
    
        private String prefix;
        
        private String suffix;
    }

4.编写业务逻辑类

    @Data
    public class HelloService {
    
        private HelloProperties helloProperties;
    
        public String sayHello(String name) {
            return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
        }
    }

5.编写自动配置类

    @Configuration
    //web环境该配置类才生效
    @ConditionalOnWebApplication
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloAutoConfigure {
    
        @Autowired
        private HelloProperties helloProperties;
    
        @Bean
        public HelloService helloService() {
            HelloService helloService = new HelloService();
            helloService.setHelloProperties(helloProperties);
            return helloService;
        }
    }

6.将自动配置类配置在META‐INF/spring.factories文件中

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.zhangbao.config.HelloAutoConfigure

7.使用maven install命令先将zhangbao-spring-boot-starter-autoconfigure项目打包到仓库中

8.然后再使用maven install命令将zhangbao-spring-boot-starter项目打包到仓库中(由于该项目依赖zhangbao-spring-boot-starter-autoconfigure项目所以先将zhangbao-spring-boot-starter-autoconfigure项目打包到仓库中)

9.创建spring-boot-custom-test项目用来测试starter是否创建成功,由于步骤5中自动配置类是在web环境中才生效,所以创建的项目需要导入web依赖

10.测试项目导入zhangbao-spring-boot-starter依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 导入zhangbao-spring-boot-starter依赖 -->
    <dependency>
        <groupId>com.zhangbao</groupId>
        <artifactId>zhangbao-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

11.创建测试Controller
    @RestController
    public class HelloController {
    
        //此时注入的就是zhangbao-spring-boot-starter-autoconfigure项目中的HelloService
        @Autowired
        private HelloService helloService;
    
        @GetMapping("/hello")
        public String hello(String name) {
    
            return helloService.sayHello(name);
        }
    }

12.在application.yml配置文件中添加步骤3中HelloProperties配置类的prefix和suffix

    spring:
      zhangbao:
        hello:
          prefix: hello
          suffix: welcome

13.启动spring-boot-custom-test项目,在浏览器中输入localhost:8080/hello?name=zhangbao
正常应该显示:hello-zhangbao-welcome

14.自定义Spring Boot Starter成功

15.详细代码见spring-boot-custom文件夹

标签:zhangbao,SpringBoot,自定义,spring,配置,boot,autoconfigure,Starters,starter
来源: https://blog.csdn.net/zhangbaoss/article/details/96478942

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有