如何写SpringBootStarter 通过一个例子让你掌握starter

1.spring-booter-starer是什么

Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件)进行自动配置。使用者只需要依赖相应功能的Starter,无需做过多的配置和依赖,Spring Boot就能自动扫描并加载相应的模块。
总结:

它整合了这个模块需要的依赖库;
提供对模块的配置项给使用者、并可以对配置项提供默认值,使得使用者可以不指定配置时提供默认配置项值,也可以根据需要指定配置项值;
提供自动配置类对模块内的Bean进行自动装配
例如,在Maven的依赖中加入spring-boot-starter-web就能使项目支持Spring MVC,并且Spring Boot还为我们做了很多默认配置,无需再依赖spring-web、spring-webmvc等相关包及做相关配置就能够立即使用起来。

官方提供的starter有50多种,见
Spring Boot Reference Guide

2 如何开始写一个starter

idea创建项目 -> 这个就没什么好写的 就和普通的工程的一样。

注意⚠️:

在pom.xml里面尽量import最小的依赖 千万不要像做工程一样 什么spring-boot-web这种倒入。

2.1 resources下面创建META-INFO文件夹,创建spring.factories文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6sNPrJHy-1666268895161)(img.png)]

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.chen.operationlogstarter.aspect.config.OperationLogAutoConfig

2.2 配置好上面的Config Bean

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BRzd5EQs-1666268895163)(img_1.png)]

@Configuration
@ConditionalOnClass(OperationLogConfigProperties.class)
@EnableConfigurationProperties(OperationLogConfigProperties.class)
@ComponentScan(basePackages = {"com.chen.*"})
public class OperationLogAutoConfig {

}

2.3 把项目打包

mvn clean install -> 推送到远程仓库

2.4

然后在你需要的项目里面引入坐标

        <dependency>
            <groupId>com.xxxx</groupId>
            <artifactId>operationlog-sdk</artifactId>
            <version>${operation-log-sdk}</version>
        </dependency>

通过一个例子 抽取通用功能 让你的代码优雅起来`


3.以公司需求写一个starter

一个通用日志异步保存在数据库的starter。轻微配置就ok了

operationLogStarter

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的示例,展示了如何手一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打包并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建一个简单的 Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能包含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值