spring-configuration-metadata 無法正常生成properties 問題記錄

今天碰到一個很奇葩的問題。在使用spring-boot-configuration-processor生成spring-configuration-metadata.json文件的時候發現properties域沒有正常生成。如下:

{
  "groups": [
    {
      "name": "wall",
      "type": "cn.twh.wall.config.WallConfig",
      "sourceType": "cn.twh.wall.config.WallConfig"
    }
  ],
  "properties": [],
  "hints": []
}

問題排查步驟:

1、排查pom文件是否依賴了

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

2、排查pom的build域是否使用了插件:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<proc>none</proc><!-- 該配置必須存在 --> 
		<source>${jdk.version}</source>
		<target>${jdk.version}</target>
		<encoding>utf-8</encoding>
	</configuration>
</plugin>

3、排查配置類:

package cn.twh.wall.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "wall", ignoreUnknownFields = true)
public class WallConfig {
	/** 
	 * 默認失敗率
	 */
	private int failRate;
	
	/** 
	 * 默認超時率
	 */
	private int timeOutRate;
	
	/** 
	 * 接口失敗率
	 */
	private Map<String,Integer> interfaceFailRate = new HashMap<>();
	
	/** 
	 * 接口失敗率
	 */
	private Map<String,Integer> interfaceTimeOutRate = new HashMap<>();
	
	/** 
	 * 接口失敗標誌
	 */
	private Map<String,String> interfaceFailFlag = new HashMap<>();
	
	/** 
	 * 接口超時標誌
	 */
	private Map<String,String> interfaceTimeOutFlag = new HashMap<>();

	public void setFailRate(int failRate) {
		this.failRate = failRate;
	}

	public void setTimeOutRate(int timeOutRate) {
		this.timeOutRate = timeOutRate;
	}

	public void setInterfaceFailRate(Map<String, Integer> interfaceFailRate) {
		this.interfaceFailRate = interfaceFailRate;
	}

	public void setInterfaceTimeOutRate(Map<String, Integer> interfaceTimeOutRate) {
		this.interfaceTimeOutRate = interfaceTimeOutRate;
	}

	public void setInterfaceFailFlag(Map<String, String> interfaceFailFlag) {
		this.interfaceFailFlag = interfaceFailFlag;
	}

	public void setInterfaceTimeOutFlag(Map<String, String> interfaceTimeOutFlag) {
		this.interfaceTimeOutFlag = interfaceTimeOutFlag;
	}
	/**
	 * 獲取api的失敗權重
	 * @param api
	 * @return
	 */
	public int getFaildRate(String api) {
		if(interfaceFailRate.containsKey(api)) {
			return interfaceFailRate.get(api);
		}else {
			return failRate;
		}
	}
	/**
	 * 獲取api的超時權重
	 * @param api
	 * @return
	 */
	public int getTimeOutRate(String api) {
		if(interfaceTimeOutRate.containsKey(api)) {
			return interfaceTimeOutRate.get(api);
		}else {
			return timeOutRate;
		}
	}
	
	public boolean containsTimeOutFlag(Map<?,?> data) {
		// TODO 
		return false;
	}
}

問題分析:

spring-configuration-metadata.json有生成說明插件配置無誤,properties沒有生成說明肯定問題在於配置類的寫法有問題。

百度無果,查官方文檔,發現有如下描述:

Properties are discovered through the presence of standard getters and setters with special handling for collection types (that is detected even if only a getter is present). 
The annotation processor also supports the use of the @Data, @Getter, and @Setter lombok annotations.

翻譯:通過標準的Get和Set發現Properties屬性,並且特殊處理了集合類。同時它支持了lombok的 @Data,@Getter,@Setter註解方式獲取。

由此可見,我這邊的問題是由於沒有寫標準Get方法導致的,用Ide加上Get方法,成功生成出對應Properties。

其他小夥伴可以嘗試下lombok。或許會簡單很多

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章