Java Web系統初始化時從資料庫中載入資料到文字(ibatis,spring)
阿新 • • 發佈:2019-02-17
有一些特殊情況,需要在系統初始化時載入一些配置屬性到本地文字中
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
spring.xmlpackage 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 "";
}
}
Constant.java
public interface Constant {
public static final String SYSTEM_ROOT = FileUtils.getValueByKey("SYSTEM_ROOT");
}