有一些特殊情况,需要在系统初始化时加载一些配置属性到本地文本中
web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 系统初始化从数据库中配置表加载数据到配置文件中,必须写在spring的监听器下方,否则bean对象未初始化 -->
<listener>
<listener-class>com.actions.common.listener.InitConfig</listener-class>
</listener>
InitConfig.java
package com.actions.common.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.dao.impl.ConfigDaoImpl;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
/**
* 功能:初始化配置文件,从数据库中将配置文件中的数据写到本地
* @author Adam
*
*/
public class InitConfig implements ServletContextListener{
private final Log log = LogFactory.getLog(getClass());
public void contextDestroyed(ServletContextEvent sce) {
log.info("系统停止...");
}
public void contextInitialized(ServletContextEvent sce) {
log.info("System initialization Start...");
WebApplicationContext rwp = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
ConfigDaoImpl configDaoImpl= (ConfigDaoImpl)rwp.getBean("configDaoImpl");
configDaoImpl.initConfig();
log.info("System initialization End...");
}
}
<bean id="configDaoImpl" class="com.dao.impl.ConfigDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
ConfigDaoImpl.java
package com.dao.impl;
import java.io.File;
import java.util.List;
import java.util.Properties;
import org.springframework.transaction.annotation.Transactional;
import com.pojos.MyConfig;
import com.util.FileUtils;
/**
* 功能:从数据库中将配置文件信息写入本地。
* @author Adam
*
*/
@Transactional
public class ConfigDaoImpl extends BaseDaoImpl{
String profilepath = ConfigDaoImpl.class.getClassLoader().
getResource("").getPath() + "config.txt";
Properties props = new Properties();
/**
* 功能:初始化配置文件
*/
public void initConfig(){
try {
profilepath = profilepath.replaceAll("%20", " ");
File outputFile = new File(profilepath);
outputFile.createNewFile();
//把所有可用值都写入文本
List list = this.getAllKeyAndValue();
if(list != null && list.size()>0){
String content = "";
for(int i =0 ; i < list.size(); i++ ){
MyConfig con = (MyConfig) list.get(i);
if(content.equals("")){
content = con.getCon_key()+"="+con.getCon_value();
}else{
content = content + "\r" + con.getCon_key()+"="+con.getCon_value();
}
}
FileUtils.writeFileUTF8(content,profilepath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getValueByKey(String con_key) throws Exception {
return (String) super.findByObj("MyConfig.getValueByKey", con_key);
}
public List getAllKeyAndValue() throws Exception{
return super.findAll("MyConfig.getAllKeyAndValue");
}
}
FileUtils.java
package com.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* 文件工具类
* FileUtils
* @version 1.0
*/
public class FileUtils {
static String root_path = FileUtils.class.getClassLoader().
getResource("").getPath() + "config.txt";
public static String getValueByKey(String con_key){
try {
root_path = root_path.replaceAll("%20", " ");
File file = new File(root_path);
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis,
"UTF-8"));
String line = br.readLine();
while (line!= null) {
if(line.indexOf(con_key)>=0){
String [] arr = line.split("=");
if(arr[0].equals(con_key)){
return arr[1];
}
}else{
line = br.readLine();
}
}
br.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
public interface Constant {
public static final String SYSTEM_ROOT = FileUtils.getValueByKey("SYSTEM_ROOT");
}