0

我正在使用Spring和Spring MVC開發Web項目。如何爲特定名稱選擇多個屬性

我有一個功能是3個不同的元素(可在下拉菜單中查看)相同。每個項目只有兩個參數會發生變化。我決定將這些元素和參數放在.properties文件中以允許用戶更改它們。所以例如在我的.properties我有以下內容:

FC 
fcUuid=11111111111111111 
fcTag=tag1 

AC 
itUuid=22222222222222222 
itTag=tag2 

IT 
acUuid=333333333333333333 
acTag=tag3 

目前,我能夠單獨檢索每個元素。

例如:

String communityUuid = SpringPropertiesUtil.getProperty("fcUuid"); 

SpringPropertiesUtil延伸PropertyPlaceholderConfigurer

但我的問題是:我怎麼可以檢索相對於一個元素的所有參數?

例如,用戶選擇「FC」,我的服務層如何檢索fcUuid和fcTag參數?

我當然可以這樣做:

if(param="FC"){ 
    String communityUuid = SpringPropertiesUtil.getProperty("fcUuid"); 
    String communityTag = SpringPropertiesUtil.getProperty("fcTag"); 
} else if (param="AC"){...} 

但我不想這樣做,因爲用戶可以添加元素,因此我會在每次修改代碼。

我想是這樣的:

String communityUuid = SpringPropertiesUtil.getProperties(param[0]); 
String tagUuid = SpringPropertiesUtil.getProperties(param[1]); 

甚至更​​好:

String communityUuid = SpringPropertiesUtil.getProperties(param[uuid]); 
String tagUuid = SpringPropertiesUtil.getProperties(param[tag]); 
+0

https://開頭計算器。com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object選中此項即可獲得所有屬性 – StanislavL

+0

實際上,我已將我的文件轉換爲我的'SpringPropertiesUtil'中的hashmap上課,這就是允許我一次獲得一個物業的原因。但是它對我的情況有何幫助? –

+0

你可以通過所有的map鍵,並檢查一個鍵是否以所需的前綴開頭並返回 – StanislavL

回答

0

在我的一位同事的幫助下,我設法意識到了這一點。這是我如何着手:

  • 在我.properties文件,我改變了數據格式,現在它看起來像:

    #FC 
    clientApplications[0].name=FC 
    clientApplications[0].communityId=00000000000000 
    clientApplications[0].tag=tag0 
    
    #AC 
    clientApplications[1].name=AC 
    clientApplications[1].communityId=11111111111111 
    clientApplications[1].tag=tag1 
    
    etc... 
    
  • 我創建了一個名爲ClientApplication(FC,AC豆和IT應用)具有3個屬性(名稱,communityId和標記)

  • 我創建了一個名爲ApplicationStore的類,它以ClientApplication對象的形式存儲properties文件中存在的所有應用程序並提供了一個get方法,該方法根據應用程序的名稱返回一個ClientApplication。

    @Component("applicationStore") 
    public class ApplicationStore {  
    
        private Map<String, ClientApplication> map; 
    
        public void put(String key, ClientApplication value) { 
         map.put(key, value); 
        } 
    
        public ClientApplication get(String key) { 
         return map.get(key); 
        } 
    
        public ApplicationStore() { 
         int i = 0; 
         map = new HashMap<String, ClientApplication>(); 
    
         while (SpringPropertiesUtil.getProperty("clientApplications[" + i + "].name") != null) { 
          ClientApplication ca = new ClientApplication(); 
          ca.setName(SpringPropertiesUtil.getProperty("clientApplications[" + i + "].name")); 
          ca.setCommunityId(SpringPropertiesUtil.getProperty("clientApplications[" + i + "].communityId")); 
          ca.setTag(SpringPropertiesUtil.getProperty("clientApplications[" + i + "].tag")); 
    
          map.put(ca.getName(), ca); 
    
          i++; 
         } 
        } 
    } 
    
  • 隨着我只有把它添加到我的業務層:

    @Service("aService") 
    public class AServiceImpl implements AService {  
        @Autowired 
        private ApplicationStore apps; 
    
        private String communityUuid; 
        private String communityTag; 
    
        @Override 
        public void aMethod(String appName) trhows Exception { 
         ClientApplication ca = new ClientApplication(); 
         ca = apps.get(appName); 
         communityUuid = ca.getCommunityId(); 
         communityTag = ca.getTag(); 
    
         System.out.println("Application for key " + app + " : " + ca); 
         System.out.println("communityUuid: " + communityUuid); 
         System.out.println("communityTag:" + communityTag); 
        } 
    } 
    
1

你需要定製如何處理,你需要的屬性到地圖。你可以這樣做:

#group your properites 
uiValues=\ 
    FC={fcUuid:11111111111111111},{fcTag : tag1}&&\ 
    AC={itUuid : 22222222222222222},{itTag : tag2}&&\ 
    IT={acUuid:333333333333333333},{acTag:tag3} 


@Component 
public class ConfigProperties { 
    //FC=...&&AC=....&&IT=.... 
    private static final String GROUP_SPLITTER = "&&"; 
    private static final String GROUP_VALUES_MARKER = "="; 
    private static final String START_VALUES_IN_GROUP = "{"; 
    private static final String END_VALUES_IN_GROUP = "}"; 
    private static final String VALUES_SPLITTER= ","; 
    private static final String KEY_VALUE_SPLITTER= ":"; 

     @Value("#{T(current current package .ConfigProperties). 
        decodeMap('${uiValues}')}") 
     private Map<String,Values> map; 

     /** 
     if(param="FC"){ 
     String communityUuid = SpringPropertiesUtil.getProperty("fcUuid"); 
     String communityTag = SpringPropertiesUtil.getProperty("fcTag"); 
     } 
     @Autowired 
     ConfigProperties configProperties; 

     String communityUuid = configProperties.getValue("FC","fcUuid"); 
     String communityTag = configProperties.getValue("FC","fcTag"); 
     */ 
     public String getValue(String key , String property){ 
      //add check for null 
      Values values= map.get(key); 
      if (values == null){ 
       return ""; 
      } 
      for (Tuple tuple : values.tuples){ 
       if (tuple.key.equals(property)){ 
        return tuple.value; 
       } 
      } 
      return ""; 
     } 

     public List<String> getProperties(String key){ 
      //add check for null 
      List<String> properties = new ArrayList<>(); 
      Values values= map.get(key); 
      //add check for null 
      for (Tuple tuple : values.tuples){ 
       properties.add(tuple.key); 
      } 
      return properties; 
     } 

     public static Map<String, Values> decodeMap(String value) { 
      //add validator for value format 
      boolean isValid = true; 
      if(!isValid){ 
       return new HashMap<>(); 
      } 
      Map<String, Values> map = new LinkedHashMap<>(); 
      String[] groups = value.split(GROUP_SPLITTER); 
      for (String group : groups) { 

       String[] values = splitToKeyAndValues(group.split(GROUP_VALUES_MARKER)[1]); 
       String key = group.substring(0,group.indexOf(GROUP_VALUES_MARKER)); 
       map.put(key, getValues(values)); 
      } 
      return map; 
     } 

     private static Values getValues(String[] parts) { 
      Values values = new Values(); 
      for (int i=0;i<parts.length;i++){ 
       values.tuples.add(getTuple(parts[i])); 
      } 
      return values; 
     } 

     private static Tuple getTuple(String parts) { 
      Tuple tuple = new Tuple(); 
      parts = parts.substring(1,parts.length()-1); 
      tuple.key= parts.split(KEY_VALUE_SPLITTER)[0]; 
      tuple.value= parts.split(KEY_VALUE_SPLITTER)[1]; 
      return tuple; 
     } 

     static String[] splitToKeyAndValues(String valuesInGroup) { 
      return valuesInGroup.split(VALUES_SPLITTER); 
     } 
    } 

    class Values{ 
     List<Tuple> tuples = new ArrayList<>(); 
    } 
    class Tuple{ 
     String key; 
     String value; 
    } 
+0

有趣的方法是,問題在於你將'.properties'文件轉換爲String變量,然後將其轉換爲Hashmap。我寧願將數據保持原樣並將其直接轉換爲Hashmap。我設法做到了,我會在今天下午發佈我的答案(巴黎UTC時間)。 –

+0

有很多方法可以做到這一點,我做了它,因爲它的屬性。在許多情況下,我說,因爲它的財產然後與它作爲value.i可以注入我需要作爲價值或作爲組件使用,並注入它,但我的情況我需要我自己的格式 – xyz

相關問題