手写 spring-boot-starter 实现自定义类的格式化

1、定义格式化接口规范【FormatProcessor 】

package com.tiger.starter.format;

/**
 * 格式化接口规范
 */
public interface FormatProcessor {

    //定义一个格式化的方法
     String format(T obj);
}

2.1、编写 json 格式处理器类【JsonFormatProcessor 】,实现格式化接口 FormatProcessor 

package com.tiger.starter.format;

import com.alibaba.fastjson.JSON;

/**
 * 编写 json 格式处理器
 */
public class JsonFormatProcessor implements FormatProcessor {

    @Override
    public  String format(T obj) {
        return "JsonFormatProcessor:" + JSON.toJSONString(obj);
    }
}

2.2、编写 json 格式处理器类【StringFormatProcessor 】,实现格式化接口 FormatProcessor 

package com.tiger.starter.format;

import java.util.Objects;

/**
 * 编写 String 格式处理器
 */
public class StringFormatProcessor implements FormatProcessor {

    @Override
    public  String format(T obj) {
        return "StringFormatProcessor:" + Objects.toString(obj);
    }
}

3、编写 关联配置文件 类【TigerProperties 】,如没用到可省略

package com.tiger.starter.configuration;


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

import java.util.Map;

/**
 * 关联配置文件类
 */
@ConfigurationProperties(prefix = TigerProperties.TIGER_FORMAT_PREFIX)
public class TigerProperties {

    public static final String              TIGER_FORMAT_PREFIX = "tiger.format";
    private             Map info;
    private             String              name;

    public String getName() {
        return name;
    }

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

    public Map getInfo() {
        return info;
    }

    public void setInfo(Map info) {
        this.info = info;
    }
}

4、编写对外的格式模板类【TigerFormatTemplate】

package com.tiger.starter;

import com.tiger.starter.configuration.TigerProperties;
import com.tiger.starter.format.FormatProcessor;

/**
 * 对外提供的格式模板类
 * 格式化模板,根据条件选择对应的模板
 */
public class TigerFormatTemplate {
    //格式处理器
    private FormatProcessor formatProcessor;
    //配置文件属性
    private TigerProperties tigerProperties;

    public TigerFormatTemplate(TigerProperties tigerProperties, FormatProcessor formatProcessor) {
        this.tigerProperties = tigerProperties;
        this.formatProcessor = formatProcessor;
    }

    /**
     * 根据传入的类型,自动对其格式化
     *
     * @param obj
     * @param 
     * @return
     */
    public  String doFormat(T obj) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("====== 开始格式化 ======").append("
"); stringBuilder.append("====== 读取配置文件信息 ======info:【").append(formatProcessor.format(tigerProperties.getInfo())).append("】"); stringBuilder.append("--name:【").append(formatProcessor.format(tigerProperties.getName())).append("】
"); stringBuilder.append("====== 格式结果【").append(formatProcessor.format(obj)).append("】
"); return stringBuilder.toString(); } }

5、条件注入配置类【FormatConditionalAutoConfiguration 】

package com.tiger.starter.configuration;


import com.tiger.starter.format.FormatProcessor;
import com.tiger.starter.format.JsonFormatProcessor;
import com.tiger.starter.format.StringFormatProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 条件注入
 */
@Configuration
public class FormatConditionalAutoConfiguration {

    /**
     * 不存在【com.alibaba.fastjson.JSON】时 ,注入
     *
     * @return
     */
    @ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
    @Bean
    @Primary //主要的
    public FormatProcessor stringFormat() {
        return new StringFormatProcessor();
    }

    /**
     * 存在【com.alibaba.fastjson.JSON】时注入
     *
     * @return
     */
    @ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
    @Bean
    public FormatProcessor jsonFormat() {
        return new JsonFormatProcessor();
    }

}

6、编写 Bean 配置类【TigerBeanAutoConfiguration 】

package com.tiger.starter.configuration;


import com.tiger.starter.TigerFormatTemplate;
import com.tiger.starter.format.FormatProcessor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Bean 配置类
 */
@Import(FormatConditionalAutoConfiguration.class)//导入模板配置类
@EnableConfigurationProperties(TigerProperties.class)//自动注入配置信息
@Configuration//标识是一个配置类
public class TigerBeanAutoConfiguration {

    @Bean
    public TigerFormatTemplate tigerFormatTemplate(TigerProperties tigerProperties, FormatProcessor formatProcessor) {
        return new TigerFormatTemplate(tigerProperties, formatProcessor);
    }
}

7、编写元信息文件,SPI【src/main/resources/META-INF/spring.factories】

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.tiger.starter.configuration.TigerBeanAutoConfiguration

8、pom文件

 




  4.0.0

  com.tiger.starter
  format-spring-boot-starter
  1.0-SNAPSHOT

  format-spring-boot-starter
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
  

  

    
      org.springframework.boot
      spring-boot-starter
      2.1.6.RELEASE
    

    
        org.springframework.boot
        spring-boot-configuration-processor
        2.1.6.RELEASE
      true
    

    
      com.alibaba
      fastjson
      1.2.56
      true
    

  

  
    format-spring-boot-starter
    
      
        src/main/java
        
          **/*.properties
          **/*.xml
        
      
      
        src/main/resources
        
          **/*.*
        
      
    

    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          8
          8
        
      
    

  

9、打包后,该jar包便可以被引入到其他springboot项目中使用,例如在pom文件中引入改信息

    
         
        
        
            com.tiger.starter
            format-spring-boot-starter
            1.0-SNAPSHOT
        

    

10、其他springboot项目配置文件中写入配置信息【src/main/resources/application.properties】

tiger.format.info.country=中国
tiger.format.info.province=广东
tiger.format.info.city=广州
tiger.format.info.region=天河
tiger.format.name=hello

或者【src/main/resources/application.yml】

tiger:
  format:
    info:
      country: 中国
      province: 广东
      city: 广州
      region: 天河
    name: hello tiger

 

你可能感兴趣的:(springboot)