Java 从属性文件使用Guava创建对象

Java 从属性文件使用Guava创建对象,java,collections,properties,guava,Java,Collections,Properties,Guava,在我们的应用程序中,我们经常使用属性文件。几个月以来,我开始学习番石榴,实际上我非常喜欢它 创建地图的最佳方法是什么 属性文件格式不严格。如果可以用另一种格式更好地表达,它可以更改吗 示例属性文件: datasource1.url=jdbc:mysql://192.168.11.46/db1 datasource1.password=password datasource1.user=root datasource2.url=jdbc:mysql://192.168.11.45/db2 data

在我们的应用程序中,我们经常使用属性文件。几个月以来,我开始学习番石榴,实际上我非常喜欢它

创建
地图的最佳方法是什么

属性文件格式不严格。如果可以用另一种格式更好地表达,它可以更改吗

示例属性文件:

datasource1.url=jdbc:mysql://192.168.11.46/db1
datasource1.password=password
datasource1.user=root
datasource2.url=jdbc:mysql://192.168.11.45/db2
datasource2.password=password
datasource2.user=root

Properties类是HashTable的一个子类,它反过来实现Map

您可以像往常一样加载以下内容:

Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) { 
}
编辑:确定,因此要将其转换为映射;)

//首先将属性转换为映射
Map m=Maps.fromProperties(属性);
//对它们进行排序,以便每个数据源和数据源1.*[密码、url、用户]
函数propToList=新函数(){
@凌驾
公共字符串应用(Map.Entry){
return entry.getKey().split(“\\”)[0];
}
};
Multimap nameToParamMap=Multimaps.index(m.entrySet(),propToList);
//将其转换为地图
Map mm=nameToParamMap.asMap();
//将其转换为地图
Map mSD=Maps.transformEntries(mm,新EntryTransformer(){
公共数据源转换项(字符串键、集合值){
//创建您的数据源。您现在知道集合实际上是一个列表,所以您可以假定元素是有序的:[密码、url、用户]
返回新数据源(…)
}
};
//复制变换后的地图,使其不再是视图
Map finalMap=Maps.newHashMap(mSD);
可能有一个更简单的方法,但这应该可以做到:)

不过,最好还是使用json或xml。您还可以从不同的文件加载不同数据源的属性

edit2:少吃番石榴,多吃爪哇:

//Sort them so that password < url < user for each datasource and dataSource1.* < dataSource2.*. In your case default string ordering is ok so we can take a normal SortedSet
SortedSet <String> sorted = new SortedSet<String>();
sorted.putAll(m.keySet);

//Divide keys into lists of 3
Iterable<List<String>> keyLists = Iterables.partition(sorted.keySet(), 3);


Map<String, Datasource> m = new HashMap<String, Datasource>();
for (keyList : keyLists) {
    //Contains datasourcex.password, datasroucex.url, datasourcex.user
    String[] params = keyList.toArray(new String[keyList.size()]);
    String password = properties.get(params[0]);
    String url = properties.get(params[1]);
    String user = properties.get(params[2]);
    m.put(params[0].split("\\.")[0], new DataSource(....)
}
//对它们进行排序,以便每个数据源和数据源1.*
如果用于配置的文件不严格,可以使用XML文件存储定义

示例定义:

<resources>
 <configuration>
   <datasrouce>
     <connection name="" url="" password="" user=""/>
     <connection name="" url="" password="" user=""/>
   </datasource>
 </configuration>
</resources>


使用连接管理器类,您可以读取XML以获取连接信息,创建连接实例并对其进行管理。

最简单的方法可能是使用JSON,而不是使用属性文件:

{ "datasources": [ { "name": "datasource1", "url": "jdbc:mysql://192.168.11.46/db1", "user": "root", "password": "password" }, { "name": "datasource2", "url": "jdbc:mysql://192.168.11.46/db2", "user": "root", "password": "password" } ] } { “数据源”:[ { “名称”:“数据源1”, “url”:“jdbc:mysql://192.168.11.46/db1", “用户”:“根用户”, “密码”:“密码” }, { “名称”:“数据源2”, “url”:“jdbc:mysql://192.168.11.46/db2", “用户”:“根用户”, “密码”:“密码” } ] } 然后,您可以使用库(如)将其转换为对象:

public class DataSources {
  private List<DataSourceInfo> dataSources;

  public Map<String, DataSource> getDataSources() {
    // create the map
  }
}

public class DataSourceInfo {
  private String name;
  private String url;
  private String user;
  private String password;

  // constructor, getters
}
公共类数据源{
私有列表数据源;
公共地图getDataSources(){
//创建地图
}
}
公共类数据源信息{
私有字符串名称;
私有字符串url;
私有字符串用户;
私有字符串密码;
//构造函数,getter
}
然后获取地图:

Gson gson = new Gson();
Map<String, DataSource> dataSources = gson.fromJson(/* file or stream here */,
    DataSources.class).getDataSources();
Gson-Gson=new-Gson();
Map dataSources=gson.fromJson(/*此处为文件或流*/,,
getDataSources();

我知道这一点,但在此之后,您建议如何创建Map?您可以在我的解决方案中保存几行。我保留了这些行,以便更易于阅读。我投了赞成票:),但我想再等一会儿。谢谢:)顺便说一句,很抱歉第一次问的问题不清楚。Stackoverflow忽略了它:)没问题。我添加了一个更干净的解决方案。我之所以投票,是因为我的想法和优雅的解决方案,而且我可能会使用您建议的解决方案,但为了符合问题的答案,我选择了另一个解决方案。:)顺便说一下,科林,我也是你的超级粉丝。:)谢谢你的贡献。不过,我也希望看到您的另一个精彩实现,以便更好地学习。;)@Sessizlik我想我的重点是使用合适的工具来完成这项工作。这里有一些你想读入的结构化数据。属性文件是扁平的,不能很好地表达结构。番石榴并没有改变这一事实。Gson(顺便说一句,谷歌的JSON转换库)似乎更合适,它使这一点变得非常简单。
Gson gson = new Gson();
Map<String, DataSource> dataSources = gson.fromJson(/* file or stream here */,
    DataSources.class).getDataSources();