0
点赞
收藏
分享

微信扫一扫

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


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

package com.tiger.starter.format;

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

//定义一个格式化的方法
<T> 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 <T> 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 <T> 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<String, Object> info;
private String name;

public String getName() {
return name;
}

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

public Map<String, Object> getInfo() {
return info;
}

public void setInfo(Map<String, Object> 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 <T>
* @return
*/
public <T> String doFormat(T obj) {

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("====== 开始格式化 ======").append("<br/>");
stringBuilder.append("====== 读取配置文件信息 ======info:【").append(formatProcessor.format(tigerProperties.getInfo())).append("】");
stringBuilder.append("--name:【").append(formatProcessor.format(tigerProperties.getName())).append("】<br/>");
stringBuilder.append("====== 格式结果【").append(formatProcessor.format(obj)).append("】<br/>");
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文件

 

<?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.tiger.starter</groupId>
<artifactId>format-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

<name>format-spring-boot-starter</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.6.RELEASE</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
<optional>true</optional><!--可选-->
</dependency>

</dependencies>

<build>
<finalName>format-spring-boot-starter</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>

</build>
</project>

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

    <dependencies>

<!-- 自定义starter -->
<dependency>
<groupId>com.tiger.starter</groupId>
<artifactId>format-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

</dependencies>

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

 

举报

相关推荐

0 条评论