最近在学习Java框架应用,所以从配置文件,并读取配置文件开始学习。
此处以读取SystemGlobals.properties为例
SystemGlobals.properties 中的内容如下:
SystemGlobals.java中的内容如下:
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; /** * @Description 系统变量加载类 * @ClassName SystemGlobals */ public class SystemGlobals { /** The preferences. the value of it all is String*/ private static Properties preferences = new Properties(); /** The queries. */ private static Properties queries = new Properties(); private static Map<String, Object> objectProperties = new HashMap<String, Object>(); /** * Gets the preference. * * @param key * the key * @return the preference */ public static String getPreference(String key) { String s = preferences.getProperty(key); if (s != null) s = s.trim(); return s; } /** * Gets the preference. * * @param key * the key * @param defaultValue * the default value * @return the preference */ public static String getPreference(String key, String defaultValue) { String s = preferences.getProperty(key); if (s == null) return defaultValue; else return s; } /** * Gets the preference. * * @param key * the key * @param params * the params * @return the preference */ public static String getPreference(String key, Object... params) { String message = preferences.getProperty(key); if (message != null) message = message.trim(); if (params == null || params.length == 0) return message; String[] ss = new String[params.length]; Object o = null; for (int i = 0; i < params.length; i++) { o = params[i]; if (o == null) { ss[i] = ""; } else { ss[i] = o.toString(); } } return replacePlaceHolder(message, ss); } /** * Sets the preference. * * @param key * the key * @param value * the value */ public static void setPreference(String key, String value) { if (value != null) { value = value.trim(); preferences.setProperty(key, value); } else { preferences.remove(key); } } /** * Gets the int preference. * * @param key * the key * @param defaultValue * the default value * @return the int preference */ public static int getIntPreference(String key, int defaultValue) { String s = getPreference(key); if (StringUtils.isBlank(s)) return defaultValue; else return Integer.parseInt(s); } /** * Sets the int preference. * * @param key * the key * @param value * the value */ public static void setIntPreference(String key, int value) { setPreference(key, String.valueOf(value)); } /** * Gets the long preference. * * @param key * the key * @param defaultValue * the default value * @return the long preference */ public static long getLongPreference(String key, long defaultValue) { String s = getPreference(key); if (StringUtils.isBlank(s)) return defaultValue; else return Long.parseLong(s); } /** * Sets the long preference. * * @param key * the key * @param value * the value */ public static void setLongPreference(String key, long value) { setPreference(key, String.valueOf(value)); } /** * Gets the boolean preference. * * @param key * the key * @param defaultValue * the default value * @return the boolean preference */ public static boolean getBooleanPreference(String key, boolean defaultValue) { String s = getPreference(key); if (StringUtils.isBlank(s)) return defaultValue; else return Boolean.parseBoolean(s); } /** * Sets the boolean preference. * * @param key * the key * @param value * the value */ public static void setBooleanPreference(String key, boolean value) { setPreference(key, String.valueOf(value)); } /** * Gets the sql. * * @param key * the key * @return the sql */ public static String getSql(String key) { return queries.getProperty(key); } /** * Load sql. * * @param file * the file */ public static void loadSql(String file) { try { InputStream is = SystemGlobals.class.getClassLoader() .getResourceAsStream(file); queries.load(is); } catch (IOException e) { e.printStackTrace(); } } /** * Load config. * * @param file * the file */ public static void loadConfig(String file) { try { InputStream is = SystemGlobals.class.getClassLoader() .getResourceAsStream(file); preferences.load(is); } catch (IOException e) { e.printStackTrace(); } } /** * Replace place holder. * * @param message * the message * @param params * the params * @return the string */ private static String replacePlaceHolder(String message, String[] params) { if (StringUtils.isBlank(message)) return message; if (params == null || params.length == 0) return message; Map<String, String> map = new HashMap<String, String>(); int index = -1; Pattern p = Pattern.compile("\\{(\\d+)\\}"); Matcher m = p.matcher(message); while (m.find()) { if (m.groupCount() < 1) continue; index = Integer.parseInt(m.group(1)); if (index < 0 || index >= params.length) continue; map.put(m.group(0), params[index]); } if (map.isEmpty()) return message; for (Entry<String, String> entry : map.entrySet()) { message = message.replace(entry.getKey(), entry.getValue()); } return message; } /** * The main method. * * @param args * the arguments */ public static void main(String[] args) { String s = "thia is a {2} or a {1} {0} hahah"; String[] params = { "AA", "BB", "CC" }; Map<String, String> map = new HashMap<String, String>(); int index = -1; Pattern p = Pattern.compile("\\{(\\d+)\\}"); Matcher m = p.matcher(s); while (m.find()) { if (m.groupCount() < 1) continue; index = Integer.parseInt(m.group(1)); if (index < 0 || index >= params.length) continue; map.put(m.group(0), params[index]); } for (Entry<String, String> entry : map.entrySet()) { s = s.replace(entry.getKey(), entry.getValue()); } System.out.println(s); } public static void setObjectValue(String field, Object value) { objectProperties.put(field, value); } public static Object getObjectValue(String field) { return objectProperties.get(field); } /** The Constant DB_JNDI. */ public static final String DB_JNDI = "db_jndi"; /** The Constant DB_USERNAME. */ public static final String DB_USERNAME = "db_userName"; /** The Constant DB_PASSWD. */ public static final String DB_PASSWD = "db_password"; /** The Constant DB_URL. */ public static final String DB_URL = "db_url"; /** The Constant SPRING_CONFIG. */ public static final String SPRING_CONFIG = "spring_config"; }
在web.xml中进行配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>com.cqa.config.file</param-name> <param-value>SystemGlobals.properties</param-value> </context-param> <listener> <listener-class>com.company.listener.ConfigInitializer</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
ConfigInitializer.java 会在程序不完全启动时执行,其的内容为:
import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.lang.StringUtils; import com.company.util.SystemGlobals; public class ConfigInitializer implements ServletContextListener { public void contextDestroyed(ServletContextEvent event) { ServletContext context = event.getServletContext(); } public void contextInitialized(ServletContextEvent event) { ServletContext servletContext = event.getServletContext(); String configFile = servletContext.getInitParameter("com.cqa.config.file"); if (!StringUtils.isBlank(configFile)) { configFile = configFile.trim(); SystemGlobals.loadConfig(configFile); } int useCache = SystemGlobals.getIntPreference("use_cache", 0); System.out.println("useCache=="+useCache); } }
可以看到
SystemGlobals.getIntPreference("name","default")方法并有其他方法放在该java类里了。
以后在程序中可以任意使用。