当前位置: 首页 > news >正文

SpringBoot自定义starter

SpringBoot自定义starter

1. 新建maven项目导入pom文件

以下是基本的依赖,如果用到其它东西,还可以导入其它依赖

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.0</version>
            <!--依赖不会被传递-->
            <optional>true</optional>
        </dependency>
        <!--注解配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.7.0</version>
        </dependency>
    </dependencies>

2. 映射配置文件类

package com.shaoby.mytest;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Author 
 * @Date 2024/5/26 21:26
 */
@Component
@ConfigurationProperties(prefix = "shaoby.my-test")
@PropertySource("classpath:application.properties")
public class MyTestProperties {
    private String name = "MyTest";

    private String host = "localhost";

    private String port = "8080";

    public String getName() {
        return name;
    }

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

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}

3.配置类

package com.shaoby.mytest;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 
 * @Date 2024/5/26 21:28
 */
@EnableConfigurationProperties(MyTestProperties.class)
@Configuration
public class MyTestConfig {

    private MyTestProperties properties;

    public MyTestConfig (MyTestProperties properties){
        this.properties = properties;
    }
    /**
     * 注入Test类的Bean对象到IOC容器
     * @return
     */
    @Bean
    public Test test(){
        return new Test(properties.getName(), properties.getHost(),properties.getPort());
    }
}

4.将配置类暴漏给SpringBoot

在MATE-INF目录下创建spring.factories文件,注意换行要加斜杠

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.shaoby.mytest.MyTestConfig

5.配置类提示

在MATE-INF目录下创建文件additional-spring-configuration-metadata.json,这样在其它项目导入自定义starter后,编写配置文件时可以起到提示作用。

{
  "properties": [{
    "name": "shaoby.my-test.name",
    "type": "java.lang.String",
    "description": "这是MyTest的name.",
    "defaultValue": "MyTest"
  },{
    "name": "shaoby.my-test.host",
    "type": "java.lang.String",
    "description": "这是MyTest的host.",
    "defaultValue": "localhost"
  },
    {
      "name": "shaoby.my-test.port",
      "type": "java.lang.String",
      "description": "这是MyTest的port.",
      "defaultValue": "8080"
    }]
}

6.打包,引入SpringBoot项目测试

引入starter坐标

        <dependency>
            <groupId>org.mytest</groupId>
            <artifactId>test-spring-boot-start</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

可以配置配置文件

shaoby.my-test.name=hello222

测试

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
//        SpringApplication.run(Application.class, args);
        AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(Application.class);
        Test test = c.getBean(Test.class);
        test.test();
    }
}

结果,bean对象已经注入到容器中。

自动装配成功----
name->hello222
host->localhost
port->8080

问题

我的配置文件类使用了@Component和@PropertySource(“classpath:application.properties”)注解,不然引入到springboot项目里后读取不到配置文件,不知道为啥????

相关文章:

  • 软件无线电学习-第二代移动通信系统过程理解
  • 【计算机网络】第三章——回退N帧协议
  • 上海亚商投顾:沪指震荡反弹 半导体产业链午后爆发
  • Golang | Leetcode Golang题解之第109题有序链表转换二叉搜索树
  • 层次式架构设计理论与实践
  • 初学C语言100题:经典例题节选(源码分享)
  • Moto和Inter字节序
  • 【讲解下Web前端三大主流的框架】
  • 2024爆款神器!会声会影2024旗舰版,让你的视频制作技能暴涨,不学真的亏大了!
  • 中国科技期刊卓越行动计划重点期刊
  • 推导2维镜像变换(Reflection Transform)的公式
  • 数据集007:垃圾分类数据集(含数据集下载链接)
  • 宝塔部署纯Vue项目,无后端
  • 文献分享《Microbiome and cancer》
  • 在 Visual Studio Code(VS Code)中调试 .NET Core 程序详细步骤
  • markdown画时序图的时候,如何自动显示每一条时序的序号
  • 服务器端口转发,服务器端口转发的作用、好处与坏处
  • Divisibility Part2(整除理论2)
  • PY32F002A单片机 us 延时,非常惊讶
  • 宿舍管理系统代码详解(操作界面)
  • 俄伏尔加格勒机场正式更名为斯大林格勒机场
  • 蔡澜回应“入ICU观察”称未至于病危,助理:只是老毛病
  • 牛市早报|今年第二批810亿元超长期特别国债资金下达,支持消费品以旧换新
  • 昆明破获一起算命破灾诈骗案,民警:大师算不到自己的未来
  • 中介在网上非法贩婴“一个孩子8.5万元”?丹阳警方介入
  • 多地征集农村假冒伪劣食品违法线索,全链条整治“三无”产品