如何自定义spring boot starter

简介: 快速自定义一个 spring boot starter
  • 前言

SpringBoot提供了许多官方的定义的starter场景启动器,如果没有提供呢?我们如何自定义一个starter场景启动器呢?


[官方文章地址](https://docs.spring.io/spring-boot/docs/2.5.4/reference/html/features.html#features.developing-auto-configuration.custom-starter)


  • 命名规则

官方定义的场景启动器:spring-boot-starter-xxxx

自定义的场景启动器:xxxx-spring-boot-starter

  • 创建一个自定义starter项目

官方强调到自定义starter项目需要直接或者间接spring-boot-starter依赖


  • 开始自定义步骤
  • 第一步:创建一个maven项目,引入依赖
<?xmlversion="1.0" encoding="UTF-8"?><projectxmlns="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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.fun</groupId><artifactId>acme-spring-boot-starter</artifactId><version>0.0.1-SNAPSHOT</version><name>acme-spring-boot-starter</name><description>自定义场景启动器</description><properties><java.version>1.8</java.version></properties><dependencies><!-- 自义定的starter需要直接或者间接的引用到这个spring boot的core依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- 自定义配置文件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure-processor</artifactId><optional>true</optional></dependency></dependencies></project>
  • 第二步:自定义一个xxxxAutoConfiguration
@Configuration// 表明这是一个配置类@EnableConfigurationProperties(AcmeProperties.class) // 引入配置类,并在容器中生效/*** @ConditionalOnProperty控制自动配置是否生效* 部分参数说明* prefix:配置文件的属性名前缀* havingValue:比较获取到的属性值与该值指定的值相同的时候才加载配置* matchIfMissing:缺少该配置属性值是否可以加载,true可以。false不可以,默认是false* name:enabled 需要在引入的依赖匹配到enabled属性才能配置生效*/@ConditionalOnProperty(prefix="acme", name="enabled", matchIfMissing=true)
publicclassAcmeAutoConfiguration {
@AutowiredprivateAcmePropertiesacmeProperties;
@Bean@ConditionalOnMissingBean(AcmeService.class) // 当容器中没有这个bean时候再注册publicAcmeServiceacmeService() {
returnnewAcmeService(acmeProperties);
    }
}
  • 第三步:写完了自动配置类需要在resource目录下创建一个目录和文件
META-INFA/spring.factories

在spring.factories文件中添加自动配置类,SpringBoot在启动的时候会扫描所有目录下spring.factories中的配置项进行自动加载

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.fun.acmespringbootstarter.config.AcmeAutoConfiguration
  • 第四步:配置类
@ConfigurationProperties(prefix="acme")
publicclassAcmeProperties {
privateStringname;
publicStringgetName() {
returnname;
    }
publicvoidsetName(Stringname) {
this.name=name;
    }
}
  • 最后一步:将自定义的starter项目打成jar包。创建一个SpringBoot将自定义starer jar的maven坐标引入pom.xml 调用即可
@RestController@RequestMapping("/test")
publicclassTestController {
@AutowiredprivateAcmeServiceacmeService;
@GetMapping("/acme")
publicvoidtestAcme() {
acmeService.print();
    }
}
acme:  enabled: true  name: fun

   调用结果

自定义starer...fun

总结:自此一个简单的自定义starter创建完成,在熟悉SpringBoot的自动配置原理情况下,就更好理解这个过程了。

[自定义starter项目](https://gitee.com/fun_zhang/acme-spring-boot-starter)


相关文章
|
17天前
|
XML Java 数据格式
Springboot中自定义组件
Springboot中自定义组件
|
17天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
69 0
|
17天前
|
Java Spring 容器
【Java】Spring如何扫描自定义的注解?
【Java】Spring如何扫描自定义的注解?
45 0
|
17天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
15天前
|
Java
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
|
17天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
25 2
|
17天前
|
Java 测试技术 开发者
【亮剑】通过自定义注解实现Spring AOP,可以更灵活地控制方法拦截和增强
【4月更文挑战第30天】通过自定义注解实现Spring AOP,可以更灵活地控制方法拦截和增强。首先定义自定义注解,如`@MyCustomAnnotation`,然后创建切面类`MyCustomAspect`,使用`@Pointcut`和`@Before/@After`定义切点及通知。配置AOP代理,添加`@EnableAspectJAutoProxy`到配置类。最后,在需拦截的方法上应用自定义注解。遵循保持注解职责单一、选择合适保留策略等最佳实践,提高代码可重用性和可维护性。记得测试AOP逻辑。
|
17天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
17天前
|
Java Maven 数据库
Spring Boot Starter: 快速简明地创建Spring应用
Spring Boot Starter: 快速简明地创建Spring应用
|
17天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
35 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置