SpringBoot入门三十四,自定义Springboot Starter
  PDnN7dVP3WHp 2023年12月22日 77 0

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_springboot

完整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_springboot_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_java_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_springboot_04

7.2 方式二

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

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

8.编译打包

maven-->clear-->install即可

SpringBoot入门三十四,自定义Springboot Starter_java_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_springboot_08

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

TestStarterService.getPropertiesInfo

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

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

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

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月22日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   46   0   0 Java
  8s1LUHPryisj   2024年05月17日   42   0   0 Java
  aRSRdgycpgWt   2024年05月17日   44   0   0 Java
PDnN7dVP3WHp