手写Starter

一、 Starter工程的命名

Spring 官方定义的Starter通常命名遵循的格式为spring-boot-starter-{name},例如 spring-boot-starter-web。Spring官方建议,非官方Starter命名应遵循{name}-spring-boot-starter的格式,例如,dubbo-spring-boot-starter

二、需求

实现的功能是:为用户提供的字符串添加前辍与后辍,而前辍与后辍定义在yml或properties配置文件中。

例如,用户输入的字符串为China,application.yml配置文件中配置的前辍为$$$,后辍为+++,则最终生成的字符串为

$$$China+++

三、实现

(1)创建工程

创建一个Spring Boot工程,命名为wrap-spring-boot-starter,并导入Configuration Processor与Lombok依赖

(2)定义一个Service类

(3)定义配置属性封装类

指定当前类用于封装来自于Spring Boot核心配置文件中的以some.service开头的beore与after属性值。即用于封装配置文件中的如下属性值

  • some.service.before
  •  someservice.after

(4)定义自动配置类

增加一个功能:为some.service再增加一个组装开关,一个boolean属性enable,当enable属性值为true时,或没有设置some.service.enable属性时才进行组装,若enable为false,则不进行组装

(5)创建spring.factories文件

在resources/META-INF目录下创建一个名为spring.factories的文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.abc.config.SomeServiceAutoConfiguration

四、使用starter

在本地要使用我们自定义的Starter,首先要保证其已经被Install到了本地Maven库

(1) 创建工程

创建一个Spring Boot工程,仅需要一个web依赖

(2)导入自定义Starter依赖

(3)定义properties.yml

自定义Starter中的属性在配置文件中也是有自动提示功能的

(4) 定义Controller

(5) 运行访问

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

2014Team

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值