private Map<String, AbstractController> messageTypeControllerMap;
private static final String CONTROLLERS_CONFIG_PATH = "config/controller/controllers.json";
/**
* 从文件中获取 controller的配置:优先级、消息类型、类名
*/
public void init(ApplicationContext app) {
this.messageTypeControllerMap = new HashMap<String, AbstractController>();
try {
String json = FileUtils.readFileToString(new File(CONTROLLERS_CONFIG_PATH));//gson很方便
List<Map<String, String>> controllerConfigList = GSON.fromJson(json,
new TypeToken<List<Map<String, String>>>() {
}.getType());
for (Map<String, String> map : controllerConfigList) {//遍历文件,取值
String messageType = map.get("messageType");
int priority = Integer.parseInt(map.get("priority"));
String className = map.get("className");
if (null == messageType || null == className) {
throw new Exception("字段缺失!");
}
Class<?> cls = Class.forName(className);
AbstractController controller = (AbstractController) app.getBean(cls);
controller.setPriority(priority);
controller.setMessageType(messageType);
this.messageTypeControllerMap.put(messageType, controller);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
文件输入、输出流:
https://blog.csdn.net/caixiexin/article/details/6719407
Properties:
读取java配置文件,继承自hashtable
方法:
getProperty(String key)通过key得到value
load(InputStream inStream)从输入流中读取属性列表(键和元素对)
setProperty(String key,String value)调用hashtable的put方法,设置键值对
store(OutputStream out,String comments)将键值对写入到指定文件中
clear()清除装载的键值对
https://www.cnblogs.com/bakari/p/3562244.html
读取文件:https://blog.csdn.net/Senton/article/details/4083127
1/InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p= new Properties();
p.load(in);
2/ResourceBundle rb = ResourceBundle.getBundle(name,Local.getDefault());
3/InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4/InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5/InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
6/InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p =new Properties();
p.load(in);
7/javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
http://www.runoob.com/java/java-properties-class.html
利用GSON
public void init() {
this.properties = new Properties();
try {
this.properties.load(new FileInputStream(APPLICATION_PROPERTIES_PATH));//路径
} catch (IOException e) {
e.printStackTrace();
GiskaLogger.log(null, GiskaLogger.EXCEPTION, this.getClass().getName() + ":init",
"加载文件[" + APPLICATION_PROPERTIES_PATH + "]异常[" + e + "].");
System.exit(-1);
}
}
String json = FileUtils.readFileToString(new File(ROLE_OPERATIONS_PATH));
this.roleOperationListMap = Constants.SINGLE_GSON.fromJson(json,
new TypeToken<Map<String, List<String>>>() {
}.getType());
String platformJson = FileUtils.readFileToString(new File(PLATFORMS_PATH));
this.namePlatformMap = Constants.SINGLE_GSON.fromJson(platformJson,
new TypeToken<Map<String, Platform>>() {
}.getType());