Ho diversi file di proprietà per la distribuzione in più ambienti in mule. Nel mio src / main / risorse che ho local.properties
e test.properties
File. Ho anche un segnaposto di proprietà globale a cui faccio riferimento mule-app.properties
come indicato in https://docs.mulesoft.com/mule-user-guide/v/3.6/deploying-to-multiple-environments cambiando solo quella variabile d'ambiente segnaposto dipendente dal server che uso.
Quindi ad esempio nel local.properties
file che potrei avere:
username=John
password=local
per test.properties
Avrei:
username=Christi
password=test
e nella mia app-mule.properties vorrei indicare:
mule.env=local or mule.env=test
Quindi in realtà funziona bene. Ma quando devo accedere a queste proprietà in una classe Java come ad esempio Config.java
non funziona. Vorrei ottenere le proprietà come in questo esempio:
public class Config {
static Properties prop = new Properties();
static {
// load a properties file
try {
InputStream input = Config.class.getClassLoader().getResourceAsStream("mule-app.properties");
prop.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static final String USERNAME = prop.getProperty("username");
public static final String PASSWORD = prop.getProperty("password");
}}
e questa classe java funziona bene se definisco tutte le proprietà direttamente nel file mule-app.properties
file, invece di fare riferimento a uno specificofile delle proprietà. Quindi le mie domande sono: come posso ottenere questo codice Java per accedere alle proprietà definite nei file di proprietà locali e testare semplicemente accedendo al riferimento in mule-app.properties
?
MODIFICARE: La mia soluzione che funziona, suggerita da @bigdestroyer:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
static Properties prop = new Properties();
static {
// load a properties file
try {
InputStream input = Config.class.getClassLoader().getResourceAsStream("mule-app.properties");
prop.load(input);
String type = prop.getProperty("mule.env");
input = Config.class.getClassLoader().getResourceAsStream(type + ".properties");
prop.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static final String USERNAME = prop.getProperty("username");
public static final String PASSWORD = prop.getProperty("password");
}}
risposte:
1 per risposta № 1Se non ti fraintendessi, potresti fare così:
public class Config {
static Properties prop = new Properties();
static {
// load a properties file
try {
InputStream input = Config.class.getClassLoader().getResourceAsStream("mule-app.properties");
InputStream input =
prop.load(input);
String type = prop.getProperty("mule.env"); //<-- here you get local or test
input = getClass().getClassLoader().getResourceAsStream(type + ".properties"); // here you get the file
prop.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static final String USERNAME = prop.getProperty("username");
public static final String PASSWORD = prop.getProperty("password");
}}
Innanzitutto, ottieni il file "typ" local
o test
, quindi si carica il file corretto.
NOTA: "m" riciclo "il input
e prop
variabili, immagino non ci siano problemi. Provalo e basta.
Spero possa essere d'aiuto.