JAva読み出しpropertiesプロファイル
, SSM , .properties , :
一.jdkで提供するjava.util.Propertiesクラス
java.util.HashTable, Map , , , put、putAll , put String value, Object 。 java.util.Properties getProperty() setProperty() , store save( ) ( .properties )。 , , :load loadFromXML。
load :load(InputStream inStream)、load(Reader reader), , 。
InputStream, :
1. getResourceAsStream
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");
2.
InputStream inStream = new FileInputStream(new File("filePath"));
3. ,
InputStream in = ClassLoader.getSystemResourceAsStream("filePath");
4. servlet , context InputStream
InputStream in = context.getResourceAsStream("filePath");
5. URL
URL url = new URL("path");
InputStream inStream = url.openStream();
読み取り方法は次のとおりです.
Properties prop = new Properties();
prop.load(inStream);
String key = prop.getProperty("username");
//String key = (String) prop.get("username");
二.Javaを通ります.util.ResourceBundleクラスの読み取りは、Propertiesを使用するよりも便利です.
1. ResourceBundle.getBundle()
ResourceBundle , properties .properties , 。
//test , com.mmq , src , test
ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");
String key = resource.getString("username");
2. InputStream
InputStream , 。
ResourceBundle resource = new PropertyResourceBundle(inStream);
: , , , :test.properties com.mmq , com/mmq/test.properties( Properties ) com/mmq/test( ResourceBundle ); src , test.properties test 。
三.インスタンスResourceLoader.java package com.bijian.study;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public final class ResourceLoader {
private static ResourceLoader loader = new ResourceLoader();
private static Map<String, Properties> loaderMap = new HashMap<String, Properties>();
private ResourceLoader() {
}
public static ResourceLoader getInstance() {
return loader;
}
public Properties getPropFromProperties(String fileName) throws Exception {
Properties prop = loaderMap.get(fileName);
if (prop != null) {
return prop;
}
String filePath = null;
String configPath = System.getProperty("configurePath");
if (configPath == null) {
filePath = this.getClass().getClassLoader().getResource(fileName).getPath();
} else {
filePath = configPath + "/" + fileName;
}
prop = new Properties();
prop.load(new FileInputStream(new File(filePath)));
loaderMap.put(fileName, prop);
return prop;
}
}
PropertiesUtil.java package com.bijian.study;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* ConcurrentMap key-value
*/
public class PropertiesUtil {
private static ResourceLoader loader = ResourceLoader.getInstance();
private static ConcurrentMap<String, String> configMap = new ConcurrentHashMap<String, String>();
private static final String DEFAULT_CONFIG_FILE = "test.properties";
private static Properties prop = null;
public static String getStringByKey(String key, String propName) {
try {
prop = loader.getPropFromProperties(propName);
} catch (Exception e) {
throw new RuntimeException(e);
}
key = key.trim();
if (!configMap.containsKey(key)) {
if (prop.getProperty(key) != null) {
configMap.put(key, prop.getProperty(key));
}
}
return configMap.get(key);
}
public static String getStringByKey(String key) {
return getStringByKey(key, DEFAULT_CONFIG_FILE);
}
public static Properties getProperties() {
try {
return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Constant.java package com.bijian.study;
public class Constant {
public static final String TEST = PropertiesUtil.getStringByKey("test", "test.properties");
}
Main.java package com.bijian.study;
public class Main {
public static void main(String[] args) {
System.out.println(Constant.TEST);
}
}