SpringBoot系列之自定义starter实践教程

Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一个工程,有需要时候直接引入项目就可以,比如需要使用rabbitMQ,直接引入spring-boot-starter-activemq既可,详细介绍可以参考Springboot官方文档关于starters的介绍

查看官方文档,可以找到如下的命名规范:

其意思是SpringBoot官方的starter命名要定义为spring-boot-starter-*,自定义或者说第三方的要命名为thirdpartyproject-spring-boot-starter

  • 官方命名空间

    • 前缀:“spring-boot-starter-”
    • 模式:spring-boot-starter-模块名
    • 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
  • 自定义命名空间
    • 后缀:“-spring-boot-starter”
    • 模式:模块-spring-boot-starter
    • 举例:mybatis-spring-boot-starter

ok,SpringBoot是可以自定义一些starter来使用的,可以用于方便项目开发,本博客以例子的方式介绍:

新建一个没有过多依赖的SpringBoot工程,spring-boot-starter必须引入,其它的可以Junit可以去掉,application类等等都不用,建议先新建一个Empty project,再引入对应配置的方式进行工程创建

pom.xml参考:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.springboot</groupId>
    <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>custom-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    </dependencies>

</project>

模仿其它starter,新建Properties配置类

package com.example.springboot.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "custom.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;
    }
}

编写业务测试类:

package com.example.springboot.starter.service;

import com.example.springboot.starter.HelloProperties;

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();
    }
}

编写自定义的自动配置类:

package com.example.springboot.starter;

import com.example.springboot.starter.service.HelloService;
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;

/**
 * <pre>
 *  自定义的自动配置类
 * </pre>
 *
 * <pre>
 * @author nicky.ma
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2020/01/02 14:31  修改内容:
 * </pre>
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

必须新建META-INF/spring.factories,然后加入如下配置,自动配置类才可以被扫描到,具体原因可以参考我之前的源码学习笔记:Springboot源码学习专栏

org.springframework.boot.autoconfigure.EnableAutoConfiguration=  com.example.springboot.starter.HelloServiceAutoConfiguration

example比较简单,starter就创建好了,接着需要新建一个web工程来实践,如图maven配置,引入custom-spring-boot-starter-autoconfigurer依赖

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

        <dependency>
            <groupId>com.example.springboot</groupId>
            <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

新建一个接口测试一下即可:

@Autowired
    HelloService helloService;

@GetMapping(value = {"/sayHello/{name}"})
    @ResponseBody
    public String sayHello(@PathVariable("name")String name){
        return helloService.sayHello(name);
    }

运行项目后,返回链接即可:http://localhost:8082/web/sayHello/testname

例子代码下载:github下载链接

原文地址:https://www.cnblogs.com/mzq123/p/12141938.html

时间: 2024-08-30 05:46:42

SpringBoot系列之自定义starter实践教程的相关文章

SpringBoot 系列 - 自己写starter

原文地址: https://www.xncoding.com/2017/07/22/spring/sb-starter.html 前言: Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增.在传统Maven项目中通常将一些层.组件拆分为模块来管理, 以便相互依赖复用,在Spring Boot项目中我们则可以创建自定义Spring Boot Starter来达成该目的. 可以认为starter是一种服务--使得使用某个功能的开发者不需要关注各种依赖库的处

SpringBoot学习(2) - 自定义starter

自己开发一个spring boot starter的步骤1.新建一个项目(全部都基于maven),比如新建一个spring-boot-starter-redis的maven项目 pom.xml: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="

SpringBoot系列之快速创建项目教程

本博客简介一下SpringBoot快速创建工程的方法,主要介绍一下Spring Initializer,Spring Initializer是IntelliJ IDEA才集成的一种快速创建SpringBoot项目的模式 选择File->New->Project->找到Spring Initializer选项,注意要联网,因为idea也是对接SpringBoot官网的.ok,选择好jdk之后,就可以点next 注意:这里的type可以是maven项目,也可以是gradle项目,maven

SpringBoot系列教程web篇之404、500异常页面配置

接着前面几篇web处理请求的博文,本文将说明,当出现异常的场景下,如404请求url不存在,,403无权,500服务器异常时,我们可以如何处理 原文友链: SpringBoot系列教程web篇之404.500异常页面配置 I. 环境搭建 首先得搭建一个web应用才有可能继续后续的测试,借助SpringBoot搭建一个web应用属于比较简单的活; 创建一个maven项目,pom文件如下 <parent> <groupId>org.springframework.boot</gr

SpringBoot系列教程web篇之重定向

原文地址: SpringBoot系列教程web篇之重定向 前面介绍了spring web篇数据返回的几种常用姿势,当我们在相应一个http请求时,除了直接返回数据之外,还有另一种常见的case -> 重定向: 比如我们在逛淘宝,没有登录就点击购买时,会跳转到登录界面,这其实就是一个重定向.本文主要介绍对于后端而言,可以怎样支持302重定向 I. 环境搭建 首先得搭建一个web应用才有可能继续后续的测试,借助SpringBoot搭建一个web应用属于比较简单的活; 创建一个maven项目,pom文

springboot核心技术(四)-----Docker、数据访问、自定义starter

Docker 1.简介 Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使 用这个镜像: 运行中的这个镜像称为容器,容器启动是非常快速的. 2.核心概念 docker主机(Host):安装了Docker程序的机器(Docker直接安装在操作系统之上): docker客户端(Client):连接docker主机进行操作: docker仓库(Registry):用来保存各种打包好的软件

SpringBoot 系列教程之事务隔离级别知识点小结

SpringBoot 系列教程之事务隔离级别知识点小结 上一篇博文介绍了声明式事务@Transactional的简单使用姿势,最文章的最后给出了这个注解的多个属性,本文将着重放在事务隔离级别的知识点上,并通过实例演示不同的事务隔离级别下,脏读.不可重复读.幻读的具体场景 I. 基础知识 在进入正文之前,先介绍一下事务隔离级别的一些基础知识点,详细内容,推荐参考博文 mysql 之锁与事务 1. 基本概念 以下基本概念源于个人理解之后,通过简单的 case 进行描述,如有问题,欢迎拍砖 更新丢失

SpringBoot 系列教程之编程式事务使用姿势介绍篇

SpringBoot 系列教程之编程式事务使用姿势介绍篇 前面介绍的几篇事务的博文,主要是利用@Transactional注解的声明式使用姿势,其好处在于使用简单,侵入性低,可辨识性高(一看就知道使用了事务):然而缺点也比较明显,不够灵活,稍不注意,可能就因为姿势不对,导致事务不生效 本文将介绍另外一种事务的使用姿势,借助TransactionTemplate的编程式事务 I. 配置 本篇主要介绍的是jdbcTemplate+transactionTemplate来完成一个编程式事务的实例 de

SpringBoot 系列教程之事务不生效的几种 case

SpringBoot 系列教程之事务不生效的几种 case 前面几篇博文介绍了声明式事务@Transactional的使用姿势,只知道正确的使用姿势可能还不够,还得知道什么场景下不生效,避免采坑.本文将主要介绍让事务不生效的几种 case I. 配置 本文的 case,将使用声明式事务,首先我们创建一个 SpringBoot 项目,版本为2.2.1.RELEASE,使用 mysql 作为目标数据库,存储引擎选择Innodb,事务隔离级别为 RR 1. 项目配置 在项目pom.xml文件中,加上s