2016-06-16 5 views
2

Frühling hat es so unglaublich einfach gemacht, Anwendungseigenschaften einzurichten ... aber wie würdest du es ohne Frühling machen?Java-Umgebung-spezifische Eigenschaften OHNE Frühling

Ich muss eine Java/Groovy-Anwendung auf einem Server bereitstellen, wo die Verwendung von Spring nicht in Frage kommt ... und ich habe auch nicht die Freiheit, etwas wie Redis zu installieren. Eine Option, die ich in Erwägung ziehe, ist, einen Spring Cloud Config Server an einem anderen Ort einzurichten und meine Anwendung Eigenschaften vom Konfigurationsserver zu verwenden. Das Problem ist, das ist jetzt ein bisschen Overkill für mein Projekt.

Kann jemand einen Weg vorschlagen, dies in gutem, altem Java zu tun? :)

+0

Verwenden Klasse [ 'java .util.Properties'] (http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html), um eine '* .properties' Datei zu laden. – Jesper

+0

Übergeben Sie eine Umgebungsvariable und laden Sie je nach Wert die eine oder andere Eigenschaftendatei? –

+0

Können Sie eine Eigenschaftendatei in Ihrer .jar-Datei bündeln oder möchten Sie die Eigenschaften nach der Bereitstellung ändern? – VGR

Antwort

0

Werfen Sie einen Blick auf http://constretto.org. Das ist einfach zu Konfigurations-Framework zu verwenden.

1

Dies ist ein wirklich einfaches und einfaches Beispiel, aber Sie können es ändern, wie Sie möchten:

PropertyConfigurator.java

public class PropertiesConfigurator 
{ 

    Properties properties = new Properties(); 

    String configInputPath = null; 

    InputStream configInputStream = null; 



    public PropertiesConfigurator(String configInputPath) 
    { 
     this.configInputPath = configInputPath; 
    } 



    public PropertiesConfigurator load() throws IOException, PropertyException 
    { 
     try 
     { 

      this.configInputStream = new FileInputStream(this.configInputPath); 

      // load a properties file 
      this.properties.load(this.configInputStream); 

      validate(); 

     } 
     catch (IOException ex) 
     { 
      System.out.println("Failed load properties file: " + this.configInputPath); 
      throw ex; 
     } 
     catch (PropertyException ex) 
     { 
      System.out.println("One or more properties are empty"); 
      throw ex; 
     } 
     finally 
     { 
      if (this.configInputStream != null) 
      { 
       try 
       { 
        this.configInputStream.close(); 
       } 
       catch (IOException ex) 
       { 
        System.out.println("Failed to close input stream"); 
        throw ex; 
       } 
      } 
     } 

     return this; 
    } 



    private void validate() throws PropertyException 
    { 
     Enumeration<?> e = this.properties.propertyNames(); 
     while (e.hasMoreElements()) 
     { 
      String key = (String) e.nextElement(); 
      String value = this.properties.getProperty(key); 

      if (value.isEmpty()) 
      { 
       System.out.println(String.format("Property %s is empty!", key)); 
       throw new PropertyException("One or more properties are empty"); 
      } 
     } 
    } 



    public String getProperty(String key) 
    { 
     return this.properties.getProperty(key); 
    } 



    @Override 
    public boolean equals(Object o) 
    { 
     if (this == o) 
      return true; 
     if (!(o instanceof PropertiesConfigurator)) 
      return false; 

     PropertiesConfigurator that = (PropertiesConfigurator) o; 

     if (properties != null ? !properties.equals(that.properties) : that.properties != null) 
      return false; 
     if (configInputPath != null ? !configInputPath.equals(that.configInputPath) : that.configInputPath != null) 
      return false; 
     return configInputStream != null ? 
       configInputStream.equals(that.configInputStream) : 
       that.configInputStream == null; 

    } 



    @Override 
    public int hashCode() 
    { 
     int result = properties != null ? properties.hashCode() : 0; 
     result = 31 * result + (configInputPath != null ? configInputPath.hashCode() : 0); 
     result = 31 * result + (configInputStream != null ? configInputStream.hashCode() : 0); 
     return result; 
    } 
} 

PropertyException.java

public class PropertyException extends Exception 
{ 
    public PropertyException() 
    { 
    } 



    public PropertyException(String message) 
    { 
     super(message); 
    } 



    public PropertyException(String message, Throwable throwable) 
    { 
     super(message, throwable); 
    } 
} 

MainRunner.java

public class MainRunner 
{ 

    public static void main(String[] args) 
    { 
     try 
     { 
      String configFilePath = "application.properties"; 
      PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator(configFilePath).load(); 

      String prop1 = propertiesConfigurator.getProperty("keyprop1"); 

      // Do whatever you want with prop1 
      // ... 
     } 

     catch (PropertyException ex) 
     { 
      System.out.println("Failed to load properties"); 
      System.exit(1); 
     } 

     catch (Exception ex) 
     { 
      System.out.println("Error in main application"); 
      System.exit(1); 
     } 
    } 
} 

Beispiel application.properties

keyprop1=value1 
keyprop2=value2 

Wieder ist es sehr einfach und Sie sollten auf jeden Fall den Code verbessern und Ihre Logik hinzufügen, Validierung etc.

+0

Danke Avihoo für die schnelle Antwort. Es ist ein großartiges Beispiel. :) Ich werde es definitiv in meinem Projekt verwenden. Ich versuche immer noch herauszufinden, wo die Umgebung bestimmt werden kann. Ich weiß nicht, wie die Leute vor dem Frühling gelebt haben ... LOL –