spring boot 自定义starter示例

springboot 约定规范

Starter项目的命名规范

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

Starter项目的结构规范(重要)

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

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来自定义我们的自动配置的类。

注意!!!:springboot 2.7 版本有所变化,具体请查看springboot官网。

hello-spring-boot-starter
在这里插入图片描述

pom.xml

依赖说明

  1. spring-boot-configuration-processor : 编译时依赖 可以帮助我们生成属性类和配置元数据,并且设置为可选依赖,避免传递给其他项目。
  2. spring-boot-starter : 基础依赖 提供了Spring Boot核心功能和默认配置
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 用户依赖必须指定的参数 -->
    <groupId>org.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!--        jdk版本-->
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- 基础依赖  提供了Spring Boot核心功能和默认配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.18</version>
        </dependency>
        <!-- 编译时依赖 可以帮助我们生成属性类和配置元数据,并且设置为可选依赖,避免传递给其他项目。-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.7.18</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

Starter项目的属性类

在创建一个自定义的Starter项目时,我们需要编写一个属性类,用来定义我们要集成的功能模块所需的配置项,并且使用@ConfigurationProperties注解来指定用户配置文件中的前缀。

package com.hello.starter.properties;

/**
 *  * 描述:配置信息 实体
 * @author yss
 * @date 2024/5/1
 */
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "demo") // 指定 用户配置文件中的前缀
public class DemoProperties {
    private String sayWhat;
    private String toWho;

    public String getSayWhat() {
        return sayWhat;
    }

    public void setSayWhat(String sayWhat) {
        this.sayWhat = sayWhat;
    }

    public String getToWho() {
        return toWho;
    }

    public void setToWho(String toWho) {
        this.toWho = toWho;
    }
}

Starter项目的业务功能类

在创建一个自定义的Starter项目时,我们需要编写一个或多个业务功能类,用来实现我们要集成的功能模块的具体逻辑。

package com.hello.starter.service;

/**
 * @author yss
 * @date 2024/5/1
 */
public class DemoService {
    public String sayWhat;
    public String toWho;
    public DemoService(String sayWhat, String toWho){
        this.sayWhat = sayWhat;
        this.toWho = toWho;
    }
    public String say(){
        return this.sayWhat + "!  " + toWho;
    }
}

Starter项目的自动配置类(重要)

在创建一个自定义的Starter项目时,我们需要编写一个自动配置类,用来根据属性类和业务功能类,创建相应的Bean对象。Springboot自动配置原理源码解读

  1. @EnableConfigurationProperties : 启用属性类,并将其注入到配置类中。
  2. @ConditionalOnProperty: 判断用户配置文件中是否有相应的配置项,存在并且符合期望则满足条件
  3. @Configuration : 标识这是一个配置类,用来创建和注册Bean对象。
  4. @Bean: 根据属性类和业务功能类,创建相应类型的Bean对象,并注册到应用上下文中。
  5. @ConditionalOnClass: 判断业务功能类是否存在
package com.hello.starter.config;

import com.hello.starter.properties.DemoProperties;
import com.hello.starter.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yss
 * @date 2024/5/1
 */
@Configuration // 标识这是一个配置类,用来创建和注册Bean对象。
//启用属性类,并将其注入到配置类中。
@EnableConfigurationProperties(DemoProperties.class) 
// 判断业务功能类是否存在
@ConditionalOnClass(DemoService.class)
// 判断用户配置文件中是否存在指定的属性(isopen),
// 如果存在并且值与期望相符, 则满足条件(开启相应功能)。
@ConditionalOnProperty(
        prefix = "demo",
        name = "isopen",
        havingValue = "true"
)  
public class DemoConfig {
    @Autowired
    private DemoProperties demoProperties;
    
	// 根据属性类和业务功能类,创建相应类型的Bean对象,并注册到应用上下文中。
    @Bean(name = "demo")  
    public DemoService demoService(){
        return new DemoService(demoProperties.getSayWhat(), 
        demoProperties.getToWho());
    }
}

Starter项目的自动装配文件(重要)

在resources/META-INF目录下创建一个名为spring.factories的文件,用来指定我们的自动配置类,让Spring Boot能够在启动时自动扫描并加载它。以下是一个示例:

#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hello.starter.config.DemoConfig

打包发布

执行 mvn clean install 命令 一个自定义的starter就完成了。

做完上面这几步,我们自定义Starter就完成了,下面我们来测试一下

引入依赖

在我们需要的项目中引入依赖。

<!--        自定义Starter-->
<dependency>
    <groupId>org.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

在这里插入图片描述

application.yml

demo:
  # 允许 demo作为bean注入IOC容器  一定要指明 isopen  不然项目无法启动
  isopen: true
  say-what: hello
  to-who: shf

DemoController.java

package com.example.testStarter;

import com.hello.starter.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yss
 * @date 2024/5/1
 */
@RestController
public class DemoController {

    @Resource(name = "demo")
    private DemoService demoService;

    @RequestMapping("/say")
    public String sayWhat(){
        return demoService.say();
    }
}

启动项目并且运行
在这里插入图片描述

ok了!!

自定义starter简要步骤
优雅地自定义Starter注解配置说明

boot3版本的自定义Starter

  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的Spring Boot自定义Starter示例,该Starter实现了一个自定义的HelloWorld功能: 1. 创建Maven项目 首先,我们需要创建一个Maven项目作为我们自定义Starter的项目。在项目的pom.xml中添加Spring Boot的依赖,以及其他需要集成的依赖。 2. 编写自动配置类 在src/main/java目录下创建一个名为HelloWorldAutoConfiguration的类,该类用于自动配置HelloWorld功能: ```java @Configuration @ConditionalOnClass(HelloWorldService.class) @EnableConfigurationProperties(HelloWorldProperties.class) public class HelloWorldAutoConfiguration { @Autowired private HelloWorldProperties properties; @Bean @ConditionalOnMissingBean public HelloWorldService helloWorldService() { HelloWorldService service = new HelloWorldService(); service.setMsg(properties.getMsg()); return service; } @ConfigurationProperties(prefix = "hello.world") public static class HelloWorldProperties { private String msg = "Hello, world!"; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } } ``` 上述代码中,@Configuration注解表示该类是一个配置类,@ConditionalOnClass注解表示只有当HelloWorldService类存在时才进行配置,@EnableConfigurationProperties注解表示将HelloWorldProperties类注入到Spring容器中。在helloWorldService方法中,我们通过读取HelloWorldProperties中的配置来创建一个HelloWorldService实例。 3. 编写Starter类 在src/main/java目录下创建一个名为HelloWorldStarter的类,该类用于将自动配置类注入到Spring容器中: ```java @Configuration @EnableConfigurationProperties(HelloWorldProperties.class) @Import(HelloWorldAutoConfiguration.class) public class HelloWorldStarter { } ``` 上述代码中,@Configuration注解表示该类是一个配置类,@EnableConfigurationProperties注解表示将HelloWorldProperties类注入到Spring容器中,@Import注解表示将HelloWorldAutoConfiguration类注入到Spring容器中。 4. 打包和发布Starter 在命令行中运行以下命令,将自定义Starter打包成jar包: ``` mvn clean package ``` 然后将jar包发布到Maven仓库中。 5. 在项目中使用自定义Starter 在其他Spring Boot项目中的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.example</groupId> <artifactId>hello-world-starter</artifactId> <version>1.0.0</version> </dependency> ``` 在项目中使用以下代码来测试自定义Starter是否生效: ```java @RestController public class TestController { @Autowired private HelloWorldService helloWorldService; @GetMapping("/hello") public String hello() { return helloWorldService.sayHello(); } } ``` 上述代码中,我们通过@Autowired注解注入了HelloWorldService实例,并在hello方法中调用了sayHello方法来测试自定义Starter是否生效。 以上就是Spring Boot自定义Starter的一个简单示例,通过自定义Starter,我们可以将自己的功能快速集成到Spring Boot中,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值