2016-04-15 14 views
0

Ich habe eine .properties in meinem Java-Projekt, das von Shell-Skript aktualisiert wird. Ich möchte diese Eigenschaftenwerte abrufen und sie als endgültige statische Variable verwenden. Hier ist mein .properties Code.properties als endgültige statische Variable

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

Ich weiß, ich kann mein .properties mit diesen erhalten und verwenden:

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

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

Was ich tun möchte, ist diese Schnur zu bekommen in jeder Klasse von meinem Projekt, ohne den Code in jeder Klasse hinzuzufügen.

Wie kann ich das tun? Ich habe versucht, ein Interface zu erstellen, ohne Erfolg.

Jeder von euch hat eine Idee?

Vielen Dank für Hilfe

Antwort

1

Sie es mit Singleton-Muster tun:

Beispiel:

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

Perfect:

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

Sie diesen Code überall aufrufen können! Danke :) – MxfrdA

Verwandte Themen