0

I am working on a web project using Spring and Spring MVC.

I have a feature that is the same for 3 different elements (which are available in dropdown in view). Only two parameters change for each item. I decided to put these elements and parameters in a .properties file to permit the user change them. So for example in my .properties I have the following:

FC
fcUuid=11111111111111111
fcTag=tag1

AC
itUuid=22222222222222222
itTag=tag2

IT
acUuid=333333333333333333
acTag=tag3

For the moment I am able to retrieve each element separately.

For example:

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

(SpringPropertiesUtil extends PropertyPlaceholderConfigurer)

But my question is: how can I retrieve all the parameters relative to one element?

For example the user selects "FC", how in my service layer can I retrieve both fcUuid and fcTag parameters?

Of course I can do something like:

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

But I don't want to do that because the user can add elements so I would have to modify the code each time.

I would like something like:

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

Or even better:

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

2 回答 2

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;
    }
于 2017-07-18T17:20:25.113 回答
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 是应用程序)的 bean,它具有 3 个属性(名称、communityId 和标签)

  • 我创建了一个名为的类,它以 ClientApplication 对象的形式ApplicationStore存储文件中存在的所有应用程序,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);
        }
    }
    
于 2017-07-19T11:33:21.987 回答