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>
String array = config.getString("config.array"); //array="10",并不是10,20,30,40
String scalar = config.getString("config.scalar"); // scalar="3,1415"
String text = config.getString("config.cite.[@text]"); // text ="To be or not to be, this is the question!"
<config>
<array>10,20,30,40</array>
<scalar>3\,1415</scalar>
<cite text="To be or not to be\, this is the question!"/>
</config>