日期:2014-05-17 浏览次数:21210 次
????? 在一些项目可能配置文件经常变化,配置文件的类型可能为xml,或者properties文件等,我们系统会自动检测到配置文件的变化,自动更新到内存等中,可以采用Apache 的commons-confiugrations实现相关的功能。如在Tomcat中web.xml配置文件的变化,tomcat会自动加载。
?????? 在commons-configuration中自动保存代码如下:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setAutoSave(true);
config.setProperty("colors.background", "#000000); // the configuration is saved after this call
?
?自动加载代码如下:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
??
?FileChangedReloadingStrategy中重点代码如下:
public boolean reloadingRequired() {
if (!reloading) {
long now = System.currentTimeMillis();
if (now > lastChecked + refreshDelay) {
lastChecked = now;
if (hasChanged())
reloading = true;
}
}
return reloading;
}
?
?
?
在web项目写一个servlet类测试属性文件变化,并自动加载。
package com.easyway.app.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
public class ConfigurationServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ConfigurationServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//从上下文中获取
PropertiesConfiguration propconfig=(PropertiesConfiguration)this.getServletContext().getAttribute("propconfig");
//打印信息到页面
String username=propconfig.getString("username");
out.println("用户的名称为:username ="+username);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
PropertiesConfiguration propconfig=(PropertiesConfiguration)this.getServletContext().getAttribute("propconfig");
String username=propconfig.getString("username");
out.println("username ="+username);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
//读取配置的文件的名称
ServletContext servletContext = this.getServletContext();
String filename=servletContext.getInitParameter("appconfig");
System.out.println("filename"+filename);
//创建自动加载的机制
PropertiesConfiguration