web.xml 配置
<listener>
<display-name>context监听器</display-name>
<listener-class>com.dyna.report.config.listeners.DynaContextListener</listener-class>
</listener>
DynaContextListener.java
package com.dyna.report.config.listeners;
import java.io.InputStream;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.dyna.report.config.abstracts.TemplateFactory;
import com.dyna.report.config.tools.H2ConfigParse;
import com.dyna.report.config.tools.LoadH2Cache;
public class DynaContextListener implements ServletContextListener {
private static Logger logger = Logger
.getLogger(DynaContextListener.class.getName());
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent event) {
logger.info("context 初始化开始******");
ServletContext context = event.getServletContext();
Map map = parseH2LoadConfig(context);
context.setAttribute("H2CacheConfig", map);
TemplateFactory fc =(TemplateFactory)(getObjectFromApplication(context, "templateFactory"));
///调用配置loadclass的方法,
LoadH2Cache cache = new LoadH2Cache();
cache.setTc(fc);
cache.loadH2Map(map);
}
/**
* 读取xml文件配置
*/
private Map parseH2LoadConfig(final ServletContext context) {
H2ConfigParse handler = new H2ConfigParse();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
InputStream conFile = context
.getResourceAsStream("/WEB-INF/conf/H2LoadConfig.xml");
SAXParser parser = factory.newSAXParser();
parser.parse(conFile, handler);
} catch (Exception e) {
e.printStackTrace();
}
Map map = handler.getH2tables();
System.out.println(" h2数据库load读取完毕,共" + map.size() + "条");
return map;
}
/**
* 根据 beanname,获取spring配置的实体bean
* @param servletContext
* @param beanName
* @return
*/
private Object getObjectFromApplication(ServletContext servletContext,
String beanName) {
// 通过WebApplicationContextUtils 得到Spring容器的实例。
ApplicationContext application = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
// 返回Bean的实例。
return application.getBean(beanName);
}
}
H2LoadConfig.xml ,内容
<?xml version="1.0" encoding="gb2312"?>
<h2>
<table name="zgcode" text="职工代码表" database="zg" loadClass="com.dyna.report.config.H2load.ZgCodeLoadCache">
<columns>
<column key="aaa102" text="代码值" type="varchar(6)" order="0"></column>
<column key="aaa103" text="代码名称" type="varchar(100)" order="1"></column>
<column key="aaa100" text="代码" type="varchar(20)" order="2"></column>
<column key="aaz093" text="代码表id" type="long" order="3"></column>
</columns>
</table>
<table name="jmcode" text="居民代码表" database="jm" loadClass="com.dyna.report.config.H2load.JmCodeLoadCache">
<columns>
<column key="aaa102" text="代码值" type="varchar(6)" order="0"></column>
<column key="aaa103" text="代码名称" type="varchar(100)" order="1"></column>
<column key="aaa100" text="代码" type="varchar(20)" order="2"></column>
<column key="aaz093" text="代码表id" type="long" order="3"></column>
</columns>
</table>
</h2>