首页 > 其他分享 >自定义SpringBoot的starter

自定义SpringBoot的starter

时间:2023-06-12 16:33:06浏览次数:57  
标签:name 自定义 class example com public starter SpringBoot

1.自定义starter名为my-starter-spring-boot-starter

1.1 idea中创建一个maven模块

groupId为com.example
artifactId为my-starter-spring-boot-starter
起名规范:
1.官方starter是spring-boot-starter-xxxx
2.自定义starter是xxx-spring-boot-starter
依赖如下

<?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>com.example</groupId>
    <artifactId>my-starter-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>my-starter</name>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

	<!-- 引入必须的autoconfigure的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies>

</project>

1.2 创建properties类,用于接收application.properties中指定的属性字段及值

@ConfigurationProperties(prefix = "com.example")
public class MyStarterProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.3 创建配置类,用于将properties类中的数据对应到配置上

public class MyStarterConfig {

    private String name;

    private MyStarterProperties myStarterProperties;

    public MyStarterConfig(MyStarterProperties myStarterProperties) {
        this.myStarterProperties = myStarterProperties;
    }

    public String getName() {
        return myStarterProperties.getName();
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.4 开启自动配置

@Configuration
@EnableConfigurationProperties(MyStarterProperties.class) // 开启配置文件生效
public class MyStarterAutoConfiguration {

    @Autowired
    MyStarterProperties myStarterProperties;

    @Bean
    @ConditionalOnMissingBean(MyStarterConfig.class)
    public MyStarterConfig myStarterConfig(){
        return new MyStarterConfig(myStarterProperties);
    }
}

1.5 resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.springboot.configuration.MyStarterAutoConfiguration

1.6 resources/META-INF/spring-configuration-metadata.json

{
  "group": [
    {
      "name": "com.example",
      "type": "com.example.springboot.MyStarterProperties",
      "sourceType": "com.example.springboot.MyStarterProperties"
    }
  ],
  "properties": [
    {
      "name": "com.example.name", // 配置变量名
      "type": "java.lang.String",
      "description": "my start name",
      "sourceType": "com.example.springboot.MyStarterProperties",
      "defaultValue": "MyStarterProperties name"
    }
  ]
}

1.5 代码树结构

image

1.6 构建编译打包到本地

clean->install

2.使用自定义starter

2.1 另一个项目中引用

<dependency>
            <groupId>com.example</groupId>
            <artifactId>my-starter-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2.2 application.properties配置文件中添加自定义的属性

com.example.name=hanboyuan

2.3 单测使用starter中的自定义配置

@RunWith(SpringRunner.class)
@Slf4j
@SpringBootTest(classes = StarterTest.class)
@SpringBootApplication(scanBasePackages = {"delta.*"},
        exclude = {DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
public class StarterTest {

    @Autowired
    MyStarterConfig myStarterConfig;

    @Test
    public void testName(){
        String name = myStarterConfig.getName();
        log.info("name:{}",name);
    }
}

标签:name,自定义,class,example,com,public,starter,SpringBoot
From: https://www.cnblogs.com/PythonOrg/p/17475365.html

相关文章

  • SpringBoot多模块项目搭建以及搭建基础模板
    多模块项目搭建目录多模块项目搭建1.父项目pom文件编辑2.创建子模块1.父项目pom文件编辑<!--1.父工程添加pom格式--><packaging>pom</packaging><!--定义子模块--><modules><module>walker-service</module><module>walker-utils&......
  • springboot使用swagger2以及遇到的一些问题
    1.导入依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId>......
  • 012 数据库学习笔记--自定义函数
    自定义函数:根据自己的需要,自定义一些函数分类:标量函数、内嵌表值函数、多声明表值函数标量函数:对单一值的操作,返回单一值;包含beginend创建的时候,指定了函数所有体,调用时也必须指定函数所有者调用时,如果函数中指定了默认值,调用的时候,可使用默认值default代替在语法上r......
  • springboot kettle gralde dockerfile 多阶段构建
    dockerfileFROMopenjdk:8-jdk-alpineASTEMP_BUILD_IMAGEENVENVREFRESH_DATE2023-06-1215:00RUNset-eux&&sed-i's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g'/etc/apk/repositoriesRUNapkupdate&&apkadd--no-cacheb......
  • 热门自定义报表系统推荐,哪款自定义报表系统更功能更强大?
    随着企业管理的日益复杂,越来越多的企业需要一款高效、灵活、易用的自定义报表系统来满足其各种报表制作和数据分析需求。然而,在众多自定义报表系统中,哪款更加强大?今天,我将向大家推荐5款热门自定义报表系统,并详细介绍其中最具代表性的VeryReport自定义报表系统。1.VeryReport编辑搜......
  • 微信小程序 自定义弹窗
    效果图wxml<viewclass="toastbd"catchtouchmove='preventTouchMove'wx:if='{{showModal}}'></view><viewclass="mytoast"catchtouchmove='preventTouchMove'wx:if='{{showModal}}'>......
  • SpringBoot自带ThreadPoolTaskScheduler实现数据库管理定时任务
    最近做了一个需求:将定时任务保存到数据库中,并在页面上实现定时任务的开关,以及更新定时任务时间后重新创建定时任务。于是想到了SpringBoot中自带的ThreadPoolTaskScheduler。在SpringBoot中提供的ThreadPoolTaskScheduler这个类,该类提供了一个schedule(Runnabletask,Triggert......
  • 医院项目中,如何使用自定义注解?
    你好,我是田哥不管是项目中,还是各种框架中,都有着大量的注解。而且,我们在项目开发中,通常一个注解就能搞定好多事情,尤其是在Spring大家族里,注解那是被玩的飞起,另外其他框架中不少使用。下面给大家罗列一下常用注解:1、@Configuration标识当前类是配置类2、@ComponentScan包扫描......
  • springboot+vue留守儿童爱心网站,附源码+数据库+论文+PPT,远程包安装运行
    1、项目介绍留守儿童爱心网站采用了B/S结构,JAVA作为开发语言,数据库采用了B/S结构,Mysql数据库进行开发。该系统包括前台操作,后台由管理员和用户两个部分,一方面,为用户提供首页、宣传新闻、志愿活动、爱心捐赠、个人中心、后台管理等功能;另一方面,为管理员提供首页、个人中心、用户管......
  • 前端 vue 自定义导航栏组件高度及返回箭头 自定义 tabbar 图标
    前端vue自定义导航栏组件高度及返回箭头自定义tabbar图标,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12986效果图如下:使用方法//page.json采用矢量图标设置返回箭头,{"path":"pages/Home/Home",......