Tệp cấu hình bao gồm các câu lệnh theo định dạng key=value
hoặc key:value
. Có thể có cách mà giá trị khóa có thể tham chiếu giá trị khóa khác. Chuỗi giữa một mở "$ {" và đóng "}" được hiểu là khóa. Giá trị của biến được thay thế có thể được định nghĩa là thuộc tính hệ thống hoặc trong tệp cấu hình.
Vì Properties
thừa hưởng từ Hashtable
, các put
và putAll
phương pháp có thể được áp dụng cho một Properties object
.
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key", "vlaue");
Properties props = new Properties();
props.putAll(map);
xây dựng các bài của @ Adam Gent trong chi tiết. 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;
}
}
tập tin cấu hình «Nếu bạn muốn tham chiếu được bỏ qua và không được thay thế sau đó bạn có thể sử dụng dưới dạng.
$${${name}} must be used for output ${ Yash }. EX: jdk = ${jre-1.8}
File: 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}}
tính Java định dạng (key = value) ví dụ log4j.properties
Bạn có thể sử dụng [Apache Commons Cấu hình] (http: //commons.apache.org/configuration/). – LaurentG
Đây có phải là câu hỏi về việc tham chiếu các thuộc tính khác trong cùng một tệp hoặc tham chiếu các thuộc tính trong các tệp khác không? –