【发布时间】:2017-07-18 09:27:57
【问题描述】:
我正在使用 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]);
【问题讨论】:
-
stackoverflow.com/questions/23506471/… 勾选此项以通过掩码获取所有属性
-
实际上我已经在
SpringPropertiesUtilclass 中将我的文件转换为hashmap,这就是允许我一次获得一个属性的原因。但它对我的情况有何帮助? -
您可以遍历所有映射键并检查键是否以所需的前缀开头并返回
标签: spring spring-mvc property-placeholder application.properties