springboot2自定义starter

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项目就创建好了。

2.添加必要的依赖。最小化配置如下。版本根据需要写,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>

3.编写自动化配置类。使用@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 {

}

4.编写自动化配置逻辑。编写一个简单的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注解详细介绍点击查看 

5.定义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>

  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个快速开发微服务的框架,它提供了大量的自动化配置和快速开发的工具。在Spring Boot中,我们可以使用Starter来快速集成一些常用的功能,如数据库、缓存、web等。而自定义Starter则可以让我们将自己的功能快速集成到Spring Boot中,下面是自定义Starter的流程: 1. 创建Maven项目 首先,我们需要创建一个Maven项目作为我们自定义Starter的项目。在项目的pom.xml中添加Spring Boot的依赖,以及其他需要集成的依赖。 2. 编写自动配置类 自动配置类是自定义Starter的核心,它负责将我们自定义的功能集成到Spring Boot中。在自动配置类中,我们可以使用@Conditional注解来判断是否需要进行配置。 3. 编写Starter类 Starter类是我们自定义Starter的入口,它负责将自动配置类注入到Spring容器中。在Starter类中,我们需要使用@EnableAutoConfiguration注解来启用自动配置。 4. 打包和发布Starter 当我们完成了自动配置类和Starter类的编写后,我们需要将自定义Starter打包成jar包,并发布到Maven仓库中,以便其他项目可以通过Maven依赖的方式使用我们的Starter。 5. 在项目中使用自定义Starter 在其他项目中使用自定义Starter非常简单,只需要在项目中的pom.xml中添加我们自定义Starter的依赖即可。Spring Boot会自动将我们的自定义Starter集成到项目中,并进行自动配置。 以上就是自定义Starter的流程,通过自定义Starter,我们可以将自己的功能快速集成到Spring Boot中,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值