如何 select 特定名称的多个属性

How to select several properties for specific name

我正在使用 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]);

您需要自定义如何将属性处理到您需要的地图中。你可以这样做:

#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;
    }

在一位同事的帮助下,我设法实现了这一点。我是这样进行的:

  • 在我的 .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 的 bean(FC、AC 和 IT 是应用程序),具有 3 个属性(名称、communityId 和标签)

  • 我创建了一个名为 ApplicationStore 的 class,它以 ClientApplication 对象的形式存储 properties 文件中存在的所有应用程序,并提供 get 方法,其中 returns 根据应用程序的名称创建 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);
        }
    }