Java Web系统初始化时从数据库中加载数据到文本(ibatis,spring)

有一些特殊情况,需要在系统初始化时加载一些配置属性到本地文本中

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...");  
	}

}


spring.xml

	<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");
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上富贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值