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

springboot自定义starter并上传至公网调用

1.目录结构

image-20210819104303353

2.新建DemoProperties实体类

package com.focusmedia.starter.Demo.properties;

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

/**
 * 在.properties中需要配置的字段
 * 配置示例:
 *      demo.sayWhat=hello
 *      demo.toWho=wshy
 *
 * @title: DingProperties
 * @Author: wshy
 */
//将实体类注册为bean
@Component
//将实体类与properties配置文件对应
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {

    private String sayWhat;
    private String toWho;

    public String getSayWhat () {
        return sayWhat;
    }

    public void setSayWhat (String sayWhat) {
        this.sayWhat = sayWhat;
    }

    public String getToWho () {
        return toWho;
    }

    public void setToWho (String toWho) {
        this.toWho = toWho;
    }

}

3.新建DemoService类

package com.focusmedia.starter.Demo.service;
/**
 * 描述:随便定义一个Service
 *      service在config中进行配置注入
 * @Author wshy
 * @Version V1.0
 **/
public class DemoService {

    public String sayWhat;
    public String toWho;

    public DemoService(String sayWhat, String toWho){
        this.sayWhat = sayWhat;
        this.toWho = toWho;
    }
    public String say(){
        return this.sayWhat + "!  " + toWho;
    }
}

4.新建DemoConfig配置类

package com.focusmedia.starter.Demo.config;

import com.focusmedia.starter.Demo.properties.DemoProperties;
import com.focusmedia.starter.Demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;

/**
 * 描述:配置类
 *
 * @Author wshy
 * @Version V1.0
 **/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
//条件注解,用于控制配置类是否生效
@ConditionalOnProperty(
        prefix = "demo",    //配置文件前缀
        name = "isopen",    //配置的名字
        havingValue = "true"//与配置中对比的对比值,当两个值想通过返回true,配置类生效
)
//条件注解,作用在类上,只有在classpath包含DemoService时,类DemoConfig中的所有@bean标注的方法才可以注入容器
@ConditionalOnClass(DemoService.class)
public class DemoConfig {
    @Autowired
    private DemoProperties demoProperties;

    @Bean(name = "demo")
    public DemoService demoService(){
        return new DemoService(demoProperties.getSayWhat(), demoProperties.getToWho());
    }
}

5.新建spring.factories

在resource下面新建MATE-INF文件夹,并新建spring.factories配置文件

#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.focusmedia.starter.Demo.config.DemoConfig

6.上传到git

image-20210819104950055

image-20210819105031784

7.Jitpack生成maven依赖

https://jitpack.io/#com.gitee.wsy-xzp/ding-spring-boot-starter/0.0.3

image-20210819105237332

image-20210819105433799

8.其他springboot服务中调用

image-20210819105906719

9.测试调用

1.application.yml中加入starter需要的配置信息

image-20210819105948317

2.启动服务,并调用接口测试

image-20210819110544454

demo地址:ding-sprig-boot-starter

相关文章:

  • 正向代理、反向代理、负载均衡
  • 64位linux编译32位程序
  • Python tips: 什么是*args和**kwargs?
  • 关于SAP UI5数据绑定我的一些原创内容
  • 安卓微信视频最佳实践
  • 触摸事件分发核心机制优化吸收
  • 异常机制详解
  • oracle 10g 搭建备库以及一次DG GAP的处理情况
  • 智能分析---表格的智能洞察
  • 线性代数学习
  • [VSCode] Shortcuts
  • Cannot retrieve metalink for repository: epel 错误解决
  • gitlab4.0_安装
  • 前端知识由零开始总结工具书
  • 从零开始学java (多态)
  • DOM的那些事
  • IDEA 插件开发入门教程
  • JSONP原理
  • Node + FFmpeg 实现Canvas动画导出视频
  • React-redux的原理以及使用
  • 设计模式走一遍---观察者模式
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • 用Visual Studio开发以太坊智能合约
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 正则与JS中的正则
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • #etcd#安装时出错
  • (0)Nginx 功能特性
  • (03)光刻——半导体电路的绘制
  • (js)循环条件满足时终止循环
  • (实战篇)如何缓存数据
  • (图)IntelliTrace Tools 跟踪云端程序
  • (一)appium-desktop定位元素原理
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • @Autowired和@Resource装配
  • @RequestBody的使用
  • @基于大模型的旅游路线推荐方案
  • [Android]Android P(9) WIFI学习笔记 - 扫描 (1)
  • [AX]AX2012开发新特性-禁止表或者表字段
  • [corCTF 2022] CoRJail: From Null Byte Overflow To Docker Escape
  • [EFI]Dell Inspiron 15 5567 电脑 Hackintosh 黑苹果efi引导文件
  • [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
  • [ExtJS5学习笔记]第三十节 sencha extjs 5表格gridpanel分组汇总
  • [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表
  • [luoguP3159] [CQOI2012]交换棋子(最小费用最大流)
  • [python开发模拟netcat工具] BHPnet
  • [Python人工智能] 四十.命名实体识别 (1)基于BiLSTM-CRF的威胁情报实体识别万字详解