Apache的Commons-configuration自动加载特性
????? 在一些项目可能配置文件经常变化,配置文件的类型可能为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 propconfig=null;try {propconfig = new PropertiesConfiguration(filename);//设置编码propconfig.setEncoding("UTF-8");//设置自动冲加载机制propconfig.setReloadingStrategy(new FileChangedReloadingStrategy());//设置到ServletContext便于使用时候获取servletContext.setAttribute("propconfig", propconfig);} catch (ConfigurationException e) {e.printStackTrace();}}}
?
?
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>appconfig</param-name> <param-value>jdbc.properties</param-value></context-param> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>configurationServlet</servlet-name> <servlet-class>com.easyway.app.servlet.ConfigurationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>configurationServlet</servlet-name> <url-pattern>/configurationServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
?