Springboot自定义starter

前言

springboot的自动配置功能能够减少我们手动配置,约定大于配置。本篇我们手动写一个starter,自定义配置实现具体的格式转化,第三方接口经常会用到JSON格式或者XML格式传输数据,根据配置是转入json还是xml格式。

我们可以看看现有的默认配置的结构:
elasticsearch
elasticsearch
kafka的默认配置:
kafka

1.引入依赖

两者选着其中之一即可。

1.1 json的转换

    <!--可以使用ali的fastjson也可以使用google的gson-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.56</version>
      <!-- 可选 -->
      <optional>true</optional>
    </dependency>

    <!--gson-->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.9</version>
      <optional>true</optional>
    </dependency>

1.2 xml转换依赖

   <!--xstream xml转换工具-->
    <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
      <version>1.4.9</version>
    </dependency>

2.定义Formate核心转化接口

/**
 * 定义核心格式转换的处理器
 * 
 */
public interface FormatProcessor {
    /**
     * 定义一个格式化的方法
     * @param obj
     * @param <T>
     * @return
     */
    <T> String formate(T obj);
}

3.实现核心接口json和xml的转换

3.1 json转换的实现

/**
 * 实现json格式的转换
 */
public class JsonFormatProcessor implements FormatProcessor{
    /**
     *
     * @param obj
     * @return
     * @param <T>
     */
    @Override
    public <T> String formate(T obj) {
        return JSON.toJSONString(obj);
    }
}

3.2 xml转换的实现

/**
 * xml转换
 */
public class XmlFormatProcessor implements FormatProcessor{
    @Override
    public <T> String formate(T obj) {
        XStream xStream= new XStream();
        return xStream.toXML(obj);
    }
}

4. FormatProperties类

/**
 * 配置类,可以配置yml或者properties配置转化的具体的格式
 */
@ConfigurationProperties(prefix = "spring.myformat")
public class FormatProperties {

    //定义默认的格式
   public static final String DEFAULT_FORMATE="com.elite.springboot.format.JsonFormatProcessor";

    /**
     * 配置外部的类型
     */
    private String format;

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }
}

5.FormatAutoConfiguration 类配置

/**
 * 核心格式转换自动配置类
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(FormatProperties.class)
public class FormatAutoConfiguration {

    //引入配置类
    private final FormatProperties properties;

    //构造器传入properties
    public FormatAutoConfiguration(FormatProperties properties) {
        this.properties = properties;
    }
    /**
     * 根据配置加载格式处理器
     * @return
     */
    @Bean
    @ConditionalOnClass(name="com.alibaba.fastjson.JSON")
    public FormatProcessor getFormatProcessor(){
        if(this.properties.getFormat()==null){
           return new JsonFormatProcessor();
        }else{
          if(this.properties.getFormat().equals("JSON")){
              return new JsonFormatProcessor();
          }else if(this.properties.getFormat().equals("XML")) {
              return new XmlFormatProcessor();
          }else{
             throw new RuntimeException();
          }
        }
    }
}

6.提供一个MyFormatTemplate 模板类

/**
 * 定义一个格式化模板类
 */
public class MyFormatTemplate {
    //加载配置格式化类
    private FormatProcessor formatProcessor;

    public MyFormatTemplate(FormatProcessor processor) {
        this.formatProcessor = processor;
    }

    /**
     * 执行格式转化
     * @param obj
     * @return
     * @param <T>
     */
    public <T> String doFormat(T obj) {
        return formatProcessor.formate(obj);
    }

}

7.注册到springboot

/**
 * 自动注册模板类
 */
@Configuration
@Import(FormatAutoConfiguration.class)
public class MyFormatAutoConfiguration {

    /**
     * 将模板类注入到bean
     * @param formatProcessor
     * @return
     */
    @Bean
    public MyFormatTemplate helloFormatTemplate(FormatProcessor formatProcessor){
        return new MyFormatTemplate(formatProcessor);
    }
}

8.创建spring.factories文件

在resources下创建META-INF目录,再在其下创建spring.factories文件
install 打包,然后就可以在SpringBoot项目中依赖改项目来操作了。

9.创建测试引入start

9.1 环境搭建

 <!--引入自定义的starter-->
    <dependency>
      <groupId>com.elite.springboot</groupId>
      <artifactId>format-spring-boot-starter</artifactId>
      <version>1.0.0</version>
    </dependency>

9.2 测试实体

public class Person {
    private String name;
    private int age;
    //省略 get set
}

9.3 controller

@RestController
public class FormatController {
    @Autowired
    MyFormatTemplate myFormatTemplate;

    @GetMapping("/format")
    public String jsonformat(){
        Person p = new Person();
        p.setName("elite");
        p.setAge(22);
        String s = myFormatTemplate.doFormat(p);
        System.out.println("s="+s);
        return s;
    }
}

JSON格式测试:
JSON

9.4 配置xml

配置yml:

server:
  port: 8888
spring:
  myformat:
    format: XML

9.5 测试

xml

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘同学要加油呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值