कॉन्फ़िगरेशन फ़ाइल प्रारूप key=value
या key:value
प्रारूप में बयान शामिल है। उनके संभावित तरीके हैं जहां एक महत्वपूर्ण मूल्य दूसरे कुंजी मान को संदर्भित कर सकता है। एक उद्घाटन "$ {" और "0" को एक कुंजी के रूप में व्याख्या किया गया है। प्रतिस्थापित चर का मान सिस्टम प्रॉपर्टी या कॉन्फ़िगरेशन फ़ाइल में ही परिभाषित किया जा सकता है।
क्योंकि Properties
inherits Hashtable
, से put
और putAll
तरीकों एक Properties object
के लिए लागू किया जा सकता है।
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key", "vlaue");
Properties props = new Properties();
props.putAll(map);
@Adam Gent की पोस्ट में विस्तृत व्याख्या। 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;
}
}
कॉन्फ़िगरेशन फ़ाइल «आप संदर्भ को नजरअंदाज किया जा करना चाहते हैं और बदला नहीं जाएगा, तो आप प्रारूप नीचे का उपयोग कर सकते हैं।
$${${name}} must be used for output ${ Yash }. EX: jdk = ${jre-1.8}
फ़ाइल: 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}}
जावा गुण (कुंजी = मान) प्रारूप उदाहरण log4j.properties
के लिए आप [अपाचे कॉमन्स विन्यास] (http: //commons.apache.org/configuration/)। – LaurentG
क्या यह एक ही फाइल में अन्य गुणों को संदर्भित करने या अन्य फ़ाइलों में गुणों का संदर्भ देने के बारे में एक प्रश्न है? –