自定义Spring Boot Starter

这篇具有很好参考价值的文章主要介绍了自定义Spring Boot Starter。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spring Boot starter

我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的。在实际项目中一些基础模块其本质就是starter,所以我们需要对Spring Boot的starter有一个全面深入的了解,这是我们的必备知识。

starter介绍

spring boot 在配置上相比spring要简单许多, 其核心在于spring-boot-starter, 在使用spring boot来搭建一个项目时, 只需要引入官方提供的starter, 就可以直接使用, 免去了各种配置。starter简单来讲就是引入了一些相关依赖和一些初始化的配置。

Spring官方提供了很多starter,第三方也可以定义starter。为了加以区分,starter从名称上进行了如下规范:

  • Spring官方提供的starter名称为:spring-boot-starter-xxx

    例如Spring官方提供的spring-boot-starter-web

  • 第三方提供的starter名称为:xxx-spring-boot-starter

    例如由mybatis提供的mybatis-spring-boot-starter

starter原理

Spring Boot之所以能够帮我们简化项目的搭建和开发过程,主要是基于它提供的起步依赖和自动配置。

起步依赖

起步依赖,其实就是将具备某种功能的坐标打包到一起,可以简化依赖导入的过程。例如,我们导入spring-boot-starter-web这个starter,则和web开发相关的jar包都一起导入到项目中了。如下图所示:

自定义Spring Boot Starter

自动配置

自动配置,就是无须手动配置xml,自动配置并管理bean,可以简化开发过程。那么Spring Boot是如何完成自动配置的呢?

自动配置涉及到如下几个关键步骤,我们可以将自动配置的关键几步以及相应的注解总结如下:

1、@Configuration与@Bean:基于Java代码的bean配置

2、@Conditional:设置自动配置条件依赖

3、@EnableConfigurationProperties与@ConfigurationProperties:读取配置文件转换为bean

4、@EnableAutoConfiguration与@Import:实现bean发现与加载

关键注解解释

@Configuration和@Bean这两个注解一起使用就可以创建一个基于java代码的配置类,可以用来替代传统的xml配置文件。

@Configuration 注解的类可以看作是能生产让Spring IoC容器管理的Bean实例的工厂。

@Bean 注解的方法返回的对象可以被注册到spring容器中。

@ConfigurationProperties注解,这个注解的作用就是把yml或者properties配置文件中的配置参数信息封装到ConfigurationProperties注解标注的bean的相应属性上。

@EnableConfigurationProperties注解的作用是使@ConfigurationProperties注解生效。

@SpringBootConfiguration:标注这个类是一个配置类; 它只是@Configuration注解的派生注解; 它与@Configuration注解的功能一致; 只不过@SpringBootConfiguration是springboot的注解,而@Configuration是spring的注解。

@ComponentScan:作用就是自动扫描并加载符合条件的组件,最终将这些bean加载到spring容器中

@EnableAutoConfiguration :这个注解很重要,借助@Import的支持,收集和注册依赖包中相关的bean定义

这些注解是spring boot特有的,常见的条件依赖注解有:

注解 功能说明
@ConditionalOnBean 仅在当前上下文中存在某个bean时,才会实例化这个Bean
@ConditionalOnClass 某个class位于类路径上,才会实例化这个Bean
@ConditionalOnExpression 当表达式为true的时候,才会实例化这个Bean
@ConditionalOnMissingBean 仅在当前上下文中不存在某个bean时,才会实例化这个Bean
@ConditionalOnMissingClass 某个class在类路径上不存在的时候,才会实例化这个Bean
@ConditionalOnNotWebApplication 不是web应用时才会实例化这个Bean
@AutoConfigureAfter 在某个bean完成自动配置后实例化这个bean
@AutoConfigureBefore 在某个bean完成自动配置前实例化这个bean

自定义 Starter 起步依赖

假设我们需要开发一个框架,配置需要用户在 properties 文件中自己定义 url 、port、username、password 四个字段,如下所示:

hello: 
  url: 10.38.233.22
  port: 8868
  username: 刘培强
  password: 123456789

这和我们自定义数据库的配置文件类似,当用户使用我们的依赖时只需要导入坐标依赖和配置如上属性即可;

我们参考starter的原理可以分为四步来实现自定义的starter依赖:

  • 创建配置属性类
  • 创建服务类
  • 创建自动配置类
  • 在resources目录下指定需要开启的自动配置类

以上四个步骤对应的模块如下面红色矩形框起来的,下面将详细讲解四个模块!
自定义Spring Boot Starter
开始之前需要导入spring的起步依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>hello-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Archetype - hello-spring-boot-starter</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath/>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>
创建配置属性类(HelloProperties)

有四个字段,和普通的实体类一样,设置get和set方法;唯一不一样的地方需要特别注意,@ConfigurationProperties(prefix = “hello”),表示这个类是配置属性类,作用是读取用户配置中的hello属性下的字段包装成一个属性类,特别注意回头看上面配置中的hello。只需要将配置中的属性字段与类中的属性一一对应即可。

package com.example.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String url;
    private String port;
    private String username;
    private String password;

    public void setUrl(String url) {
        this.url = url;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public String getPort() {
        return port;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

创建服务类(HelloService)

这个类是真正实现业务逻辑的类,它需要依赖上一步属性类HelloProperties获得的用户配置,然后实现一些功能!

package com.example.service;
public class HelloService {
    private String url;
    private String port;
    private String username;
    private String password;
    public HelloService(String url, String port, String username, String password) {
        this.url = url;
        this.port = port;
        this.username = username;
        this.password = password;
    }
    public String ConnectSystem() throws InterruptedException {
        /**
         * do something.......
         */
        System.out.println("连接中>>> "+url+":"+port);
        Thread.sleep(1000);
        System.out.println("认证中>>> "+username);
        Thread.sleep(1000);
        System.out.println("连接成功>>>"+ username +"中校您好!您已成功连接到国防系统");
        return "链接地址:"+url+":"+port+","+"访问者:"+username+"中校";
    }
}
创建自动配置类(HelloServiceAutoConfiguration)

这个类的作用就是将第一步的属性类HelloProperties的数据传给第二步的服务类HelloService的对象,并在容器中实例化该此类。需要将其指定为@Configuration 用来指明为配置类 并且用@EnableConfigurationProperties(HelloProperties.class) 开启自动配置。

package com.example.config;
import com.example.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 配置类,基于Java代码的bean配置
 * 作用:将properties的配置文件的字段自动加载到HelloProperties类中
 */
@Configuration //指明配置类
@EnableConfigurationProperties(HelloProperties.class) //开启自动配置
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloProperties helloProperties;
    //实例化HelloService并载入Spring IoC容器
    @Bean
    @ConditionalOnMissingBean //条件:容器中没有Bean才创建
    public HelloService helloService(){
        return new HelloService(helloProperties.getUrl(),helloProperties.getPort(),helloProperties.getUsername(),helloProperties.getPassword());
    }
}
在resources目录下创建META-INF/spring.factories

上一步中我们只是指定了HelloServiceAutoConfiguration为配置类,**如果这个模块不用打成jar包给其他的模块服务,那么仅需要配置类即可(即本服务在本模块中运行仅需要配置类即可)。但是,我们的这个模块需要打成jar包,因此需要在一个工厂配置中指定,才能加载此类,**因此我们需要告诉Spring容器去加载此配置类,在resources目录下创建META-INF/spring.factories并指定自动加载的配置类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.config.HelloServiceAutoConfiguration

最后使用maven将我们的自定义starter依赖进行安装,如果不会请百度!

使用自定义依赖

创建maven工程myapp并配置pom.xml文件
<!--导入自定义starter-->
<dependency>
    <groupId>cn.itcast</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
创建application.yml文件
hello:
  url: 10.38.233.22
  port: 8868
  username: 刘培强
  password: 123456789

server:
  port: 8080
创建Get请求接口
package com.example.controller;
import com.example.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/connectSystem")
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    public String hello() throws InterruptedException {
        return helloService.ConnectSystem();
    }
}
创建启动类HelloApplication
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args);
    }
}

访问链接:loaclhost:8080/connectSystem/hello
代码下载:https://download.csdn.net/download/cj151525/87944694文章来源地址https://www.toymoban.com/news/detail-499317.html

到了这里,关于自定义Spring Boot Starter的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 如何优雅地创建一个自定义的Spring Boot Starter

    优雅永不过时,希望看完本文,你会觉得starter如此优雅! Spring Boot Starter是一种简化Spring Boot应用开发的机制,它可以通过引入一些预定义的依赖和配置,让我们快速地集成某些功能模块,而无需繁琐地编写代码和配置文件。Spring Boot官方提供了很多常用的Starter,例如 spring

    2024年02月11日
    浏览(17)
  • 自定义redission装配和集成分布式开源限流业务组件ratelimiter-spring-boot-starter的正确姿势

    自定义redission装配和集成分布式开源限流业务组件ratelimiter-spring-boot-starter的正确姿势   由于使用了redisson-spring-boot-starter,在自定义redisson装配的时候会被redisson-spring-boot-starter里面的start默认装配了,同时在使用开源分布式限流组件ratelimiter-spring-boot-starter的时候,这个里面

    2024年02月07日
    浏览(14)
  • Spring Boot - spring-boot-starter

    spring-boot-starter 当学习Spring Boot时,可以通过一个完整的案例来理解和实践其基本概念和功能。以下是一个简单的Spring Boot Starter完整案例,展示了如何创建一个基本的Web应用程序: 首先,创建一个名为pom.xml的Maven项目文件,添加以下内容:idea或其他直接创建直接跳过!

    2024年02月09日
    浏览(18)
  • Spring Boot Starter设计实现

    Starter 是 Spring Boot 非常重要的一个硬核功能。 通过 Starter 我们可以快速的引入一个功能或模块,而无须关心模块依赖的其它组件。关于配置,Spring Boot 采用“约定大于配置”的设计理念,Starter 一般都会提供默认配置,只有当我们有特殊需求的时候,才需要在 application.yaml 里

    2024年01月18日
    浏览(12)
  • Spring Boot Starter Parent

    Spring Boot Starter Parent

    在这,您将学习了解 Spring Boot Starter Parent, 它是 Spring Boot 提供的父级 Pom 文件,旨在提供自动版本依赖管理,帮助我们轻松快速地进行 Spring Boot 开发。 通过 Spring Boot Starter Parent, 我们可以进行简单便捷地包依赖管理。在 Spring Boot 每一个发行版中, 均提供了该版本所兼容的依

    2024年02月08日
    浏览(8)
  • Spring Boot Starter 剖析与实践

    对于 Java 开发人员来说,Spring 框架几乎是必不可少的。它是一个广泛用于开发企业应用程序的开源轻量级框架。近几年,Spring Boot 在传统 Spring 框架的基础上应运而生,不仅提供了 Spring 的全部功能,还使开发人员更加便捷地使用。在使用 Spring Boot 时,我们经常会接触到各种

    2024年02月14日
    浏览(13)
  • Spring Boot Starter介绍和实战

    Spring Boot Starter 是 Spring Boot 提供的一种机制,用于简化和集成应用程序的依赖管理。通过创建自定义的 Starter,可以将一组相关的依赖打包成一个简单的、可重用的模块,使应用程序的配置和依赖管理更加方便。在本文中,我们将深入探讨 Spring Boot Starter 的原理、创建过程,

    2024年01月23日
    浏览(9)
  • 【Spring Boot 初识丨三】starter

    【Spring Boot 初识丨三】starter

    上一篇讲了如何构建 MAVEN 项目 本篇来讲一讲 starter 依赖项 Spring Boot 初识: 【Spring Boot 初识丨一】入门实战 【Spring Boot 初识丨二】maven 【Spring Boot 初识丨三】starter 【Spring Boot 初识丨四】主应用类   启动器是一组方便的依赖关系描述符,它包含了一系列可以集成到应用里

    2024年02月09日
    浏览(9)
  • SpringBoot进阶教程(七十九)spring-boot-starter- 有哪些 starter类型

    spring Boot应用启动器基本的一共有44种,具体如下 参考文献:https://www.javatpoint.com/spring-boot-starters

    2024年02月05日
    浏览(50)
  • 如何自己实现一个Spring Boot Starter

    如何自己实现一个Spring Boot Starter

    现在很多开源的组件都会提供对应的 springboot-starter 包给我们去用,要做一个 starter 包并不难。参照Spring内置的实现就好了: 1、在工程里引入 starter 打包相关的依赖。 2、在我们工程内建 spring.factories 文件,编写我们配置类的全限类名。 使用AOP实现拦截方法执行和打印日志的

    2024年01月22日
    浏览(8)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包