2016-12-14 15 views
0

我的应用程序有不同的客户(20左右),每个客户都有自己的*.properties文件与连接设置,属性参数相同。区分多个属性文件

目前,我已经为每个客户提供了一种自己的方法来读取属性并将其存储在Customer中。与20个客户,其充气。我正在寻找更好的解决方案。

private final static Customer get_CustomerXXXX() { 

     final Properties p = new Properties(); 

      p.load(S.class.getResourceAsStream("customerXXX.properties")); 
      return new Customer (p.getProperty("PARAM1", p.getProperty("PARAM2", p.getProperty("PARAM3") 
    } 

    if(SPECIFIC_CUSTOMER.XXXX) { 
     customerSettings = get_CustomerXXXX(); 

    } else if(SPECIFIC_CUSTOMER.BBBB) { 
     customerSettings = get_CustomerBBBB(); 
    } 
+0

*请*格式化代码时要注意。目前这一切都在这个地方。请记住,Stack Overflow的目标是成为高质量问题和答案的存储库 - 不可读的代码会降低该目标。 –

回答

0
public class CustomerTest { 
    private String identifier; 

    public CustomerTest(String identifier) { 
     this.identifier = identifier; 
    } 

    public Properties getProerties() { 
    Properties p = null; 
    try { 
      p = new Properties(); 
      p.load(CustomerTest.class.getResourceAsStream("customer" + identifier + ".properties")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return p; 
    } 
} 

这将是您在评论@OP建议的方式。

0

属性参数都是相同

,如果你确信在未来还我会是一样的。那么你只能第一次阅读属性文件。从下次病房开始,使用从以前的属性文件加载的值。

您可以在这里结合Singleton和Factory Method设计模式。

你应该有一个方法,如下列:

private final static Properties getProperties(String idetifier) 
{ 
    Properties p = new Properties(); 
    p.load(S.class.getResourceAsStream("customer"+idetifier+".properties")); 
    return p; 
} 
+0

但连接设置有些不同 – Aelop

+0

只有连接设置不同,才将连接设置放在客户特定文件中。将通用值移至单独的文件。 – Azodious

+0

'每个客户都有自己的* .properties文件和连接设置,属性参数名称与他所说的相同。据我了解,只有参数名称(键)没有不同,但值是。这意味着每个porperties文件都有像param1 = xxx和param2 = yyy。 xxx和yyy可以更改,但param1和param2总是以该名称存在。我可能是错的,但是它已经告诉我们 – Aelop