El archivo de configuración consta de declaraciones en el formato key=value
o key:value
. Son posibles formas en que un valor clave puede hacer referencia al otro valor clave. La cadena entre una apertura "$ {" y cerrando "}" se interpreta como una clave. El valor de la variable sustituida se puede definir como una propiedad del sistema o en el archivo de configuración en sí.
Debido Properties
hereda de Hashtable
, los métodos put
y putAll
se puede aplicar a un Properties object
.
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key", "vlaue");
Properties props = new Properties();
props.putAll(map);
elaborar el puesto de @ Adam Gent en-detallada. commons-text-1.1.jar
import org.apache.commons.text.StrLookup;
import org.apache.commons.text.StrSubstitutor;
public class Properties_With_ReferedKeys {
public static void main(String[] args) {
ClassLoader classLoader = Properties_With_ReferedKeys.class.getClassLoader();
String propertiesFilename = "keys_ReferedKeys.properties";
Properties props = getMappedProperties(classLoader, propertiesFilename);
System.out.println(props.getProperty("jdk"));
}
public static Properties getMappedProperties(ClassLoader classLoader, String configFilename) {
Properties fileProperties = new Properties();
try {
InputStream resourceAsStream = classLoader.getResourceAsStream(configFilename);
Map<String, String> loadPropertiesMap = loadPropertiesMap(resourceAsStream);
Set<String> keySet = loadPropertiesMap.keySet();
System.out.println("Provided 'Key':'Value' pairs are...");
for (String key : keySet) {
System.out.println(key + " : " + loadPropertiesMap.get(key));
}
fileProperties.putAll(loadPropertiesMap);
} catch (IOException e) {
e.printStackTrace();
}
return fileProperties;
}
public static Map<String,String> loadPropertiesMap(InputStream inputStream) throws IOException {
final Map<String, String> unResolvedProps = new LinkedHashMap<String, String>();
/*Reads a property list (key and element pairs) from the input byte stream.
* The input stream is in a simple line-oriented format.
*/
@SuppressWarnings("serial")
Properties props = new Properties() {
@Override
public synchronized Object put(Object key, Object value) {
unResolvedProps.put((String)key, (String)value);
return super.put(key, value);
}
};
props.load(inputStream);
final Map<String,String> resolvedProps = new LinkedHashMap<String, String>(unResolvedProps.size());
// Substitutes variables within a string by values.
StrSubstitutor sub = new StrSubstitutor(new StrLookup<String>() {
@Override
public String lookup(String key) {
/*The value of the key is first searched in the configuration file,
* and if not found there, it is then searched in the system properties.*/
String value = resolvedProps.get(key);
if (value == null)
return System.getProperty(key);
return value;
}
});
for (String key : unResolvedProps.keySet()) {
/*Replaces all the occurrences of variables with their matching values from the resolver using the given
* source string as a template. By using the default ${} the corresponding value replaces the ${variableName} sequence.*/
String value = sub.replace(unResolvedProps.get(key));
resolvedProps.put(key, value);
}
return resolvedProps;
}
}
archivo de configuración «Si quieres referencia a ser ignorado y no serán reemplazados, puede utilizar el formato de abajo.
$${${name}} must be used for output ${ Yash }. EX: jdk = ${jre-1.8}
del archivo: keys_ReferedKeys.properties
# MySQL Key for each developer for their local machine
dbIP = 127.0.0.1
dbName = myApplicationDB
dbUser = scott
dbPassword = tiger
# MySQL Properties
# To replace fixed-keys with corresponding build environment values. like « predev,testing,preprd.
config.db.driverClassName : com.mysql.jdbc.Driver
config.db.url : jdbc:mysql://${dbIP}:3306/${dbName}
config.db.username : ${dbUser}
config.db.password : ${dbPassword}
# SystemProperties
userDir = ${user.dir}
os.name = ${os.name}
java.version = ${java.version}
java.specification.version = ${java.specification.version}
# If you want reference to be ignored and won't be replaced.
# $${${name}} must be used for output ${ Yash }. EX: jdk = ${jre-1.8}
jdk = $${jre-${java.specification.version}}
propiedades Java formato (clave = valor) Ejemplo log4j.properties
se puede utilizar [Configuración Commons Apache] (http: //commons.apache.org/configuration/). – LaurentG
¿Se trata de hacer referencia a otras propiedades en el mismo archivo o hacer referencia a propiedades en otros archivos? –