当前位置:   article > 正文

案例实操|带你学会自定义SpringBoot starter启动器_web项目 自定义starter

web项目 自定义starter

.引言

相信现在有很多小伙伴都已经很熟悉SpringBoot技术了。它大大地简化了Spring应用的开发,极大地提高了项目的开发效率,受到广大开发者和企业的青睐。特别是SpringBoot官方针对各种不同的应用场景,提供了非常丰富的场景启动器(也称为起步依赖)。开发人员只需要在项目的POM文件中导入对应的场景依赖,并编写少量的配置,即可快速实现当前场景的应用开发,真正的实现开箱即用。

今天建哥会通过这篇文章,并结合一个具体的案例来给各位小伙伴介绍一下,我们该如何自定义一个自己的SpringBoot场景启动器,毕竟有时候官方提供的starter不能完全满足我们所有的需求。同时建哥也希望通过starter的自定义过程,能够加深大家对SpringBoot自动配置原理的理解。

.需求说明

我们先来看一段代码:

  1. package com.qf.hello.service;
  2. import com.qf.hello.bean.HelloProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. public class HelloService {
  5.     @Autowired
  6.     HelloProperties helloProperties;
  7.     
  8.     public String sayHello(String name){
  9.         
  10.         return helloProperties.getPrefix() + ":" +  name + ">>>"  + helloProperties.getSuffix();
  11.     }
  12. }

上面我们定义了一个组件HelloService,它有一个非常简单的功能,就是能够根据调用者传递的名字返回一个打招呼的信息,返回的信息内容可以根据配置的前缀和后缀进行指定格式的设置。我们现在需要将这个功能做成一个Starter,将来在其他项目中可以直接以场景启动器的方式导入并使用。

.设计思路

回顾我们之前使用已经做好的starter,你会发现无非就是如下几个步骤:

  1. 在POM文件中导入场景依赖;

  2. 这个场景依赖中,包含了一个名为xxxAutoConfiguration的自动配置类;

  3. 自动配置类按照一定的条件进行相关组件的自动装配

  4. 这些组件又绑定了名为xxxProperties属性配置类;

  5. 属性配置类通过指定的前缀,从application.yml配置文件中读取属性的配置信息;

  6. 最后在项目中直接使用这些配置好的组件。

我们就参考这个步骤开始进行自定义starter的操作。

.实现步骤

1.Step1 业务定义

创建一个空项目【customer-starter】,里面包含两个模块:

  • 启动器模块【hello-spring-boot-starter】;

  • 自动配置模块【hello-spring-boot-starter-configuration】

其中启动器项目中无需任何源代码和配置文件,只需要引入自动配置项目的依赖即可。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.qf</groupId>
  7.     <artifactId>hello-spring-boot-starter</artifactId>
  8.     <packaging>pom</packaging>
  9.     <version>1.0-SNAPSHOT</version>
  10.     <dependencies>
  11.         <dependency>
  12.             <groupId>com.qf</groupId>
  13.             <artifactId>hello-spring-boot-starter-configuration</artifactId>
  14.             <version>1.0-SNAPSHOT</version>
  15.         </dependency>
  16.     </dependencies>
  17. </project>

自动配置项目必须是一个SpringBoot工程,同时需要引入spring-boot-starter的依赖。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <parent>
  7.         <groupId>org.springframework.boot</groupId>
  8.         <artifactId>spring-boot-starter-parent</artifactId>
  9.         <version>2.4.5</version>
  10.     </parent>
  11.     <groupId>com.qf</groupId>
  12.     <artifactId>hello-spring-boot-starter-configuration</artifactId>
  13.     <version>1.0-SNAPSHOT</version>
  14.     <dependencies>
  15.         <dependency>
  16.             <groupId>org.springframework.boot</groupId>
  17.             <artifactId>spring-boot-starter</artifactId>
  18.         </dependency>
  19.     </dependencies>
  20. </project>

2.Step2 自动配置

编写自动配置项目中的内容。

HelloService是整个自定义Starter要装配的核心对象,HelloServiceAutoConfiguration是一个配置类,HelloProperties是HelloService组件绑定的属性配置类,他们的代码分别如下:

2.1 HelloService类

  1. //HelloService:该组件不要默认注册到容器中,而是通过一个自动配置类按条件进行装配
  2. package com.qf.hello.service;
  3. import com.qf.hello.bean.HelloProperties;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. public class HelloService {
  6.     @Autowired
  7.     HelloProperties helloProperties;
  8.     public String sayHello(String name){
  9.         return helloProperties.getPrefix() + ":" +  name + ">>>"  + helloProperties.getSuffix();
  10.     }
  11. }

2.2 HelloProperties类

  1. //HelloProperties:自配配置属性类
  2. package com.qf.hello.bean;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. @ConfigurationProperties(prefix = "hello")
  5. public class HelloProperties {
  6.     //sayHello方法使用的前缀信息
  7.     private String prefix;
  8.     //sayHello方法使用的后缀信息
  9.     private String suffix;
  10.     public String getPrefix() {
  11.         return prefix;
  12.     }
  13.     public void setPrefix(String prefix) {
  14.         this.prefix = prefix;
  15.     }
  16.     public String getSuffix() {
  17.         return suffix;
  18.     }
  19.     public void setSuffix(String suffix) {
  20.         this.suffix = suffix;
  21.     }
  22. }

2.3 HelloServiceAutoConfiguration类

  1. //自动配置类
  2. package com.qf.hello.auto;
  3. import com.qf.hello.bean.HelloProperties;
  4. import com.qf.hello.service.HelloService;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. @EnableConfigurationProperties(HelloProperties.class)
  11. public class HelloServiceAutoConfiguration {
  12.     @ConditionalOnMissingBean(HelloService.class)
  13.     @Bean
  14.     public HelloService helloService(){
  15.         System.out.println("使用自定义starter提供的HelloService");
  16.         return new HelloService();
  17.     }
  18. }

3.Step3 工厂文件

在自动配置项目中的resources目录中,提供一个名称为META-INF的目录,并在该目录下提供一个名为spring.factories的文件。

resources/META-INF/spring.factories

spring.factories配置的内容如下:

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  2. \com.qf.hello.auto.HelloServiceAutoConfiguration

4.Step4 安装

将这两个项目clean,并install到本地仓库。

5.Step5 引入使用

创建一个web项目进行自定义starter的使用测试。

5.1 在应用中添加自定义starter依赖坐标

  1. <dependency>
  2.     <groupId>com.qf</groupId>
  3.     <artifactId>hello-spring-boot-starter</artifactId>
  4.     <version>1.0-SNAPSHOT</version>
  5. </dependency>

5.2 编写配置信息

  1. server:
  2.   port: 8080
  3. hello:
  4.   prefix: 千锋
  5.   suffix: 888

5.3 编写测试的Controller

并在该Controller中自动注入自定义场景启动器中的HelloService组件。

  1. package com.qf.boot.controller;
  2. import com.qf.hello.service.HelloService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class HelloController {
  9.     @Autowired
  10.     HelloService helloService;
  11.     @GetMapping("/hello/{name}")
  12.     public String hello(@PathVariable("name") String name){
  13.         return helloService.sayHello(name);
  14.     }
  15. }

5.4 打开浏览器输入Controller中定义的访问地址

通过测试发现,我们已经可以在其他项目中使用自定义的starter,并使用自动配置好的组件功能了!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/947749
推荐阅读
相关标签
  

闽ICP备14008679号