今天看到项目中有很多AppConfig.getInstance().get("字符串xxx")。对于这个比较疑惑,后来知道。原来是项目中添加了第三方jar包
expframework.jar
具体应用查看了下源码,源码地址http://www.pudn.com/downloads89/sourcecode/java/detail339948.html
在类路径下加入app.properties这个文件,文件中的内容以KEY = VALUE的形式保存,然后在类中以AppConfig.getInstance().get(key)。的形式得到值。其实就是对properties类作了一下封装面已
以下是AppConfig类的源码
- package org.expframework;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
-
-
-
-
-
-
-
- public class AppConfig {
-
-
-
-
- private static final AppConfig config = new AppConfig();
-
-
-
-
- private Properties properties;
-
-
-
-
- private AppConfig() {
- properties = new Properties();
-
- InputStream stream = AppConfig.class
- .getResourceAsStream("/app.properties");
-
- if (stream == null) {
- throw new IllegalArgumentException(
- "Could not load app.properties. Please make sure that it is in CLASSPATH.");
- }
-
- try {
- properties.load(stream);
- } catch (IOException e) {
- IllegalStateException ex = new IllegalStateException(
- "An error occurred when reading from the input stream");
- ex.initCause(e);
- throw ex;
- } finally {
- try {
- stream.close();
- } catch (IOException e) {
- IllegalStateException ex = new IllegalStateException(
- "An I/O error occured while closing the stream");
- ex.initCause(e);
- throw ex;
- }
- }
- }
-
-
-
-
-
-
- public static AppConfig getInstance() {
- return config;
- }
-
-
-
-
-
-
-
-
-
-
- public String get(String key) {
- if (properties == null || key == null)
- return null;
- return (String) properties.get(key);
- }
- }