2016-04-15 8 views
0

私のJavaプロジェクトには.propertiesがあります。これはシェルスクリプトによって更新されています。 私はこのプロパティ値を取得し、最終的な静的変数として使用したいと思います。ここに私の.propertiesコードが。最終的な静的変数としてのプロパティ

WEBURL=http://popgom.fr 
NODEURL=http://192.168.2.30:5555/wd/hub 

である私は、私はこれを使用して、私の.propertiesファイルを入手して使用することができます知っている:

Properties prop = new Properties(); 
InputStream input = new FileInputStream("config.properties"); 
// load a properties file 
prop.load(input); 

String urlnode = prop.getProperty("NODEURL"); 

私が何をしたいのか、私のプロジェクトのすべてのクラスでは、この文字列を取得することで、すべてのクラスにコードを追加せずに。

どうすればいいですか?私は成功しないでInterfaceをやろうとしています。

アイデアはありますか?あなたはSingletonパターンを使用してそれを行うことができますヘルプ

答えて

1

ため

ありがとう:

例:あなたはどこにでもこのコードを呼び出すことができます

public class MyProperties{ 
    private static MyProperties instance = null; 
    private Properties prop; 

    private MyProperties() { 
     InputStream input = new FileInputStream("config.properties"); 
     // load a properties file 
     prop.load(input); 

    } 
    public static MyProperties getInstance() { 
     if(instance == null) { 
     instance = new MyProperties(); 
     } 
     return instance; 
    } 

    public Properties getProperty() { 
     return prop; 
    } 
} 

.... 
.... 
MyProperties.getInstance().getProperty(); 
.... 
.... 
+0

パーフェクト!ありがとう:) – MxfrdA

関連する問題