手写一个简单的starter

2024-06-10 17:12
文章标签 简单 手写 starter

本文主要是介绍手写一个简单的starter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

手写一个简单的starter

先了解一下什么是 starter:一个 starter其实就是对一个功能的集成封装,然后对外提供一个依赖,让业务去使

用,像我们熟悉的 Redis,mongo,mybatis 等均属于。

编写完starter后,可以提交到公司私有仓库供其他项目组进行调用。

1、命名规范

由于任何人都可以编写自己的 starter,为了区分官方的 starter 和个人的 starter,通常在命名上面会有一个规

范。SpringBoot 官方提出规范如下:

官方命名

作为前缀:spring-boot-starter-xxx

比如:spring-boot-starter-web…

(第三方)自定义命名

作为后缀:xxx-spring-boot-starter

比如:test-spring-boot-starter

自动装配首先要有一个配置类,其次还要有 spring.factories 文件,所以这两步是必不可少的。

2、创建流程规范

  • 定义核心业务类,这是该starter存在的意义

  • 完成自动配置类,目的是完成业务类的实例化

  • 若核心业务类中需要从配置文件获取配置数据,还需要定义一个用于封装配置文件中相关属性的类

  • 定义 META-INF/spring.factories 配置文件,用于对自动配置类进行注册。

3、starter代码编写

3.1 引入自动装配依赖

编写配置类首先要添加一个自动装配的依赖,然后再编写对应的配置类和业务实现类,在 pom 中添加如下依赖。

<?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"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.4.RELEASE</version></parent><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>my-starter</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

3.2 编写配置类

我们要编写一个提供被springboot管理的自动配置的stater,首先我们要做的是编写配置类

package com.example.starter.config;import com.example.starter.pojo.ShareDemo;
import com.example.starter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;@Configuration
@ConditionalOnClass(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {@Autowiredprivate HelloProperties properties;@Bean@ConditionalOnProperty(name = "hello.service.enable", havingValue = "true")public HelloService worldService() {return new HelloService(properties.getName());}@Bean@ConditionalOnProperty(name = "hello.service.enable", havingValue = "false")public HelloService worldService1() {return new HelloService("z");}@Bean@ConditionalOnMissingBeanpublic HelloService worldService2() {return new HelloService("vvv");}@Beanpublic ShareDemo getShareDemo(HelloProperties worldProperties) {ShareDemo shareDemo = new ShareDemo();shareDemo.setName(worldProperties.getName());return shareDemo;}
}

3.3 属性类

package com.example.starter.config;import org.springframework.boot.context.properties.ConfigurationProperties;//属性前缀,要读取配置文件中的"hello.service.name"
@ConfigurationProperties("hello.service")
public class HelloProperties {private String name;public HelloProperties() {}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

3.4 业务实现类

package com.example.starter.service;public class HelloService {public HelloService(String name) {this.name = name;}private String name;public String info() {return "your info: " + name;}}

3.5 增加配置文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.starter.config.HelloServiceAutoConfiguration

4、使用

4.1 pom依赖

<?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 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.2.4.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>use-my-starter</artifactId><version>0.0.1-SNAPSHOT</version><name>use-my-starter</name><description>use-my-starter</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 自己定义的starter --><dependency><groupId>org.example</groupId><artifactId>my-starter</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

4.2 application.properties配置文件

hello.service.name=welcome
hello.service.enable=true

4.3 测试

package com.example.usemystarter;import com.example.starter.service.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class UseMyStarterApplicationTests {@Autowiredprivate HelloService helloService;@Testvoid contextLoads() {// your info: welcomeSystem.out.println(helloService.info());}}

修改application.properties配置文件:

hello.service.name=welcome
hello.service.enable=false

结果为:your info: z

去掉application.properties配置文件中的内容,结果为:your info: vvv

这篇关于手写一个简单的starter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048765

相关文章

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的