1. 首页
  2. 软件开发
  3. JAVA
  4. springboot2自定义starter

springboot2自定义starter

  • 发布于 2024-08-16
  • 0 次阅读

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

  1. 创建 maven 项目。首先根据 springboot 约定规范,Starter 项目的命名规范如下:

建议自定义的 starter 以 xxx-spring-boot-starter 命名,官方的 Starter 一般都是以 spring-boot-starter - 为前缀。这样做的目的是为了避免与官方或其他第三方提供的 Starter 产生冲突或混淆。

项目命名规范虽然不是强制性规范,但为了方便理解,建议遵守规范 。

所以我将项目命名为 hello-spring-boot-starter。即创建 hello-spring-boot-starter 文件夹。并在文件下面创建 pom.xml 文件。

pom.xml

<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>xpl.starter.hello</groupId>
  <artifactId>hello-spring-boot-starter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

至此,一个普通的 maven 项目就创建好了。

  1. 添加必要的依赖。最小化配置如下。版本根据需要写,jdk8 只支持 springboot2。
<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>xpl.starter.hello</groupId>
  <artifactId>hello-spring-boot-starter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.6</version>
  </parent>
  
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
</project>
  1. 编写自动化配置类。使用 @ConfigurationProperties 和 @Configuration 等注解来定义配置类,这些类将用于接收和处理应用程序的配置信息。当配置文件包含 hello.msg 属性定义时自动装载:
package xpl.starter.hello;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
 
@ConditionalOnProperty(prefix = "hello",name = "msg")
public class HelloAutoConfig {
 
}
  1. 编写自动化配置逻辑。编写一个简单的 bean。

HelloService.java

package xpl.starter.hello;
 
public class HelloService {
	private String msg;
	public HelloService(String msg) {
		this.msg = msg;
	}
	
	public String sayHello(String name) {
		return name + ":"  + msg;
	}
}

在 HelloAutoConfig.java 中装配。

package xpl.starter.hello;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
 
@ConditionalOnProperty(prefix = "hello",name = "msg")
public class HelloAutoConfig {
	@Value("${hello.msg}")
	private String msg;
	
	@ConditionalOnMissingBean(HelloService.class)
	@Bean
	public HelloService helloService() {
		return new HelloService(msg);
	}
	
}

SpringBoot 之 @ConditionalOnXX 注解详细介绍点击查看

  1. 定义 Spring Boot 自动装配文件(重要)。

作用: 用来指定我们的自动配置类,让 Spring Boot 能够在启动时自动扫描并加载它。
名称必须为 spring.factories
路径必须为 resources/META-INF/spring.factories 这是 springboot2.7 之前的约定规范,不遵守一律失效。

1、在 spring boot2.7 版本之前:
通过 META-INF/spring.factories 文件定义我们自动配置的类。
2、在 spring boot2.7~spring boot3.0 版本之间,是兼容了
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 和
META-INF/spring.factories 这两个文件的。
3、在 spring boot3.0 版本之后,只支持使用
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 来自定义我们的自动配置的类。

这里我使用的是 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 来配置。

 6. 打包和发布。使用 mvn package 命令,或者直接在 eclipse 运行项目选择 maven install。

打包成功后,可以在仓库看到相关项目。

至此,自定义 starter 就完成了。其他项目要加载该 starter 只需要依赖中配置就可以了。

<dependency>
		<groupId>xpl.starter.hello</groupId>
		<artifactId>hello-spring-boot-starter</artifactId>
		<version>0.0.1-SNAPSHOT</version>
</dependency>