首页 > 其他分享 >SpringBoot入门三十四,自定义Springboot Starter

SpringBoot入门三十四,自定义Springboot Starter

时间:2023-12-20 11:04:00浏览次数:26  
标签:qfx Springboot 自定义 boot springframework starter import org Starter

1.前言

Spring Boot Starter是一种用于简化Spring Boot应用程序配置的机制。通过自定义Starter,我们可以将一组相关的配置、依赖和自动配置打包成一个可重用的模块,使得其他开发者可以轻松地集成和使用。

本篇文章将引导你创建一个简单的自定义Spring Boot Starter,并演示如何在应用程序中使用它。

2.pom.xml添加引用信息

首先我们需要创建一个Maven项目,并在pom.xml文件中添加以下依赖:

<!-- 1.引入spring-boot-autoconfigure,用于自动配置应用程序的各种组件和功能.自定义starter时必须要有 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<!-- 2.引入spring-boot-configuration-processor,自定义starter时,调用方的配置文件中可以给出提示信息(如不引用,则不会提示,此包非必须) -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <!-- 表示两个项目之间依赖不传递:不设置optional或者optional是false,表示传递依赖;当出现依赖冲突时候,会自动剔除该依赖- -->
  <optional>true</optional>
</dependency>

SpringBoot入门三十四,自定义Springboot Starter_java

完整pom.xml文件如下,springboot版本是2.7.x

<?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>
	<groupId>com.qfx.starter</groupId>
	<artifactId>qfx-test-spring-boot-starter</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>qfx-test-spring-boot-starter</name>
	<description>一个自定义的测试spring.boot.starter</description>
	
	<!-- 设置父类,整合第三方常用框架依赖信息(各种依赖信息),这里继承SpringBoot提供的父工程 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.17</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<!-- 设置公共参数 -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- Maven install 时,测试环境@Test中如果有中文输出是乱码,加上这句话试试 -->
		<argLine>-Dfile.encoding=UTF-8</argLine>
		<!-- Maven编译时的编码 -->
		<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
		<!-- 编译打包时关掉单元测试 -->
		<skipTests>true</skipTests>
		
		<!-- jdk版本 -->
		<java.version>1.8</java.version>
	</properties>
	
	<dependencies>
		<!-- 1.引入spring-boot-autoconfigure,用于自动配置应用程序的各种组件和功能.自定义starter时必须要有 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
		</dependency>

		<!-- 2.引入spring-boot-configuration-processor,自定义starter时,调用方的配置文件中可以给出提示信息(如不引用,则不会提示,此包非必须) -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<!-- 表示两个项目之间依赖不传递:不设置optional或者optional是false,表示传递依赖;当出现依赖冲突时候,会自动剔除该依赖- -->
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>

3.添加属性类

其次,如果你的Starter需要一些配置属性,你可以创建一个属性类来绑定这些属性。创建一个新的Java类,并使用@ConfigurationProperties注解标记它为属性类。在该类中,定义你需要的属性,并提供相应的getter和setter方法。

代码:

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

/**
 * <h5>功能:将配置文件中以 "qfx" 为前缀的属性值绑定到此Java类上</h5>
 * starter被引用时,会将驼峰命名的参数名称转化为全小写,并在原大写的前面添加"-",
 * 如本类的userName属性,会以qfx.user-name来进行展现
 * 
 */
@ConfigurationProperties(prefix = "qfx")
public class TestProperties {
	
	private String userName;
	private String userPwd;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
}

属性中驼峰命名的名称,在配置文件中设置的时候将转换成全小写,并以"-"来进行连接,例如上面属性类中的"userName",在引用的文件中会以"qfx.user-name"的形式来展现,如下图:

SpringBoot入门三十四,自定义Springboot Starter_java_02

4.添加自动配置类

再次,我们需要创建一个自动配置类,用于配置和初始化我们的Starter。创建一个新的Java类,并使用@Configuration注解标记它为配置类。

使用@EnableConfigurationProperties启用属性类。在该类中,我们可以定义一些Bean和自动配置逻辑。

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.qfx.module.common.properties.TestProperties;
import com.qfx.module.test.service.TestStarterService;

/**
 * <h5>功能:自动配置类</h5>
 * @ConditionalOnClass
 */
@Configuration	// 表示这个类为配置类
@EnableConfigurationProperties(TestProperties.class)	// 启用 TestProperties 类的配置属性
public class TestAutoConfig {
	// 在这里定义你要注册的对象
}

5.添加一个业务类

定义一个业务类,编写核心业务流程

import org.springframework.beans.factory.annotation.Autowired;

import com.qfx.module.common.properties.TestProperties;

/**
 * 这里不要添加@Service等类似的注解,要放在自动配置类中根据条件加载 
 */
public class TestStarterService {
	@Autowired
	private TestProperties properties;
	
  /**
   * 模拟方法,获取属性类的信息 
   */
	public String getPropertiesInfo() {
		System.out.println("模拟执行业务...");
		return properties.getUserName() + "_" + properties.getUserPwd();
	}
}

6.自动配置类添加注册对象

在第4步中添加注册对象

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.qfx.module.common.properties.TestProperties;
import com.qfx.module.test.service.TestStarterService;

/**
 * <h5>功能:自动配置类</h5>
 * @ConditionalOnClass
 */
@Configuration	// 表示这个类为配置类
@EnableConfigurationProperties(TestProperties.class)	// 启用 TestProperties 类的配置属性
public class TestAutoConfig {

	@Bean
	@ConditionalOnClass(TestStarterService.class)		// 只有当指定的类存在于类路径中时,才会创建该方法返回的 Bean
	public TestStarterService testStarterService() {
		return new TestStarterService();
	}
}

SpringBoot入门三十四,自定义Springboot Starter_springboot_03

7.添加需要自动配置的配置类信息

7.1 方式一

在resources目录下建立一个/META-INF目录,里面创建一个spring.factories文件,添加需要自动配置的配置类信息(Springboot3.0以下版本可用)

# 自动配置,如果有多个配置则使用",\"结尾的方式即可
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.qfx.module.common.config.TestAutoConfig

SpringBoot入门三十四,自定义Springboot Starter_java_04

7.2 方式二

在resources目录下建立一个/META-INF/spring目录,里面创建一个org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,  添加需要自动配置的配置类信息(Springboot2.7及以上版本可用)

SpringBoot入门三十四,自定义Springboot Starter_springboot_05

8.编译打包

maven-->clear-->install即可

SpringBoot入门三十四,自定义Springboot Starter_springboot_06

9.引用

9.1 pom.xml添加引用包

在被引用的项目中,添加生成的引用包

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

9.2 调用自动配置类中实例化的类

和平时使用一样的方式

@Autowired
TestStarterService testStarterService;

编写一个controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.qfx.module.test.service.TestStarterService;

@RestController
@RequestMapping("test")
public class TestCtl {

	@Autowired
	TestStarterService testStarterService;

	/**
	 * <h5>描述:调用starter方法,并返回数据</h5>
	 * 
	 * @return 
	 */
	@RequestMapping("starter")
	@ResponseBody
	public String starter() {
		return testStarterService.getPropertiesInfo();
	}
}

SpringBoot入门三十四,自定义Springboot Starter_springboot_07

9.3 测试

启动服务,进行测试 http://127.0.0.1:8080/test/starter

SpringBoot入门三十四,自定义Springboot Starter_java_08

自定义starter第5步中的信息:

TestStarterService.getPropertiesInfo

SpringBoot入门三十四,自定义Springboot Starter_java_09

后台输出了自定义starter第5步中的信息

SpringBoot入门三十四,自定义Springboot Starter_java_10

至此,说明自定义starter成功被调用了。

标签:qfx,Springboot,自定义,boot,springframework,starter,import,org,Starter
From: https://blog.51cto.com/abcd/8904093

相关文章

  • Qt自定义GridView从显示单个到九宫格
    一、概述由于测试OpenCV的需要自定义一个可变的用于显示图片的GridView,从显示单张图片到9张图片。效果图如下:这个GridView目前只是自己使用,还有瑕疵,这里仅提供一个可行性的思路,有需要可以自行扩展。二、代码示例1.自定义GridView--->VariableGridView.h/Vari......
  • C++ Qt开发:QItemDelegate 自定义代理组件
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍QStyledItemDelegate自定义代理组件的常用方法及灵活运用。在Qt中,QStyledItemDelegate类是用于......
  • 在Mapbox-gl-js中添加自定义图层
    在Mapbox-gl-js中添加自定义图层前言一、制作geojson地图二、使用Tippecanoe将geojson转换成vectortile(.pbf)文件三、使用mapbox-gl-js显示三、Mapbox-gl-js中根据矢量数据的属性过滤显示前言本文说明如何制作自定义的地图数据,并使用mapbox-gl-js进行显示一、制作geoj......
  • 【SpringBootWeb入门-15】Mybatis-基础操作-增改查操作
    1、章节回顾上一篇文章我们讲解了Mybatis的删除操作,本篇继续学习Mybatis的新增操作:根据员工表字段,新增员工表的数据,新增的字段有:用户名、员工姓名、性别、图像、职位、入职日期、归属部门。2、增删改查操作-新增操作员工表emp新增数据,对应的SQL语句:insertintoemp(username......
  • springboot019食品安全管理系统(vue)
    1 绪 论1.1课题研究背景及意义1.2研究现状以及发展趋势1.2.1研究现状1.2.2发展趋势1.3研究目标2相关技术介绍2.1SpringBoot介绍Spring的全家桶,我想在Java开发领域大家都知道了吧,那么关于spring的框架,自从我们大学都开始学的,Java语言在基础知识当中不会涉及到框架,但一旦学......
  • springboot020汽车改装方案网站(vue)
    1绪论1.1课题背景:当今电子信息发展十分迅猛,软件行业发展的节奏也非常的快。在我们日常的生活中有很多非常智能的软件,除此之外新兴的智能软件也如雨后春笋般的出现,人们的生活方式也一点一点的被潜移默化的改变着,当今社会的这种生活方式也是一种更人性化的“懒人”模式,比如人们在......
  • springboot016高校学生党员发展管理系统(vue,毕业设计,附数据库和源码)
    1绪论1.1研究背景1.2研究现状1.3研究意义2系统开发工具介绍2.1Springboot2.2VUE框架2.3Mysql数据库3可行性分析3.1技术的可行性3.2经济的可行性3.3操作可行性 4系统需求分析4.1系统功能需求管理系统的主要功能结构图呈现如下:图4.1高校发展学生党员管理系统功能结构......
  • Unreal入门,开灯,自定义事件
    1.创建一个点光源作为灯新建一个基于Actor的蓝图添加一个StaticMesh作为灯的外观将StaticMesh拖放到DefaultSceneRoot作为根节点添加点光源将地图的平行光调暗将刚创建的灯拖放到地图2.新建一个基于Actor的开关设置一个StaticMesh并拖放到覆盖根节点设置碰撞为......
  • 50道SpringBoot高频题整理(附答案背诵版)
    1、什么是SpringBoot?它的主要特点是什么?SpringBoot是一个开源的Java-based框架,用来简化Spring应用程序的创建和部署。它是Spring平台和第三方库的集成,可以让你很容易的创建出独立的、生产级别的、基于Spring框架的应用。SpringBoot通过提供默认配置来简化项目的配置过程,这意味......
  • uniGUI学习之自定义Hint(74)
    默认Hint样式 procedureTMainForm.UniFormShow(Sender:TObject);vari:Integer;beginfori:=0toSelf.ControlCount-1dobeginwithTUniFormControl(Self.Controls[i])dobeginifHint<>''thenbeginSh......