web启动加载servlet得到Spring bean
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class InitServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(InitServlet.class);
private static final long serialVersionUID = 6066089839055144264L;
/**
* 系统初始化参数servlet
*
* @param servletConfig
* servlet
*/
public void init(ServletConfig servletConfig) throws ServletException {
ServletContext context = servletConfig.getServletContext();
SelectAll selectAll = new SelectAll(context);
try {
selectAll.setArray();
} catch (Exception e1) {
log.error(e1.getMessage(), e1);
}
log.info("正在载入XML文件...\r\n ");
LoadXmlFile loadXmlFile = new LoadXmlFile(servletConfig);
loadXmlFile.setArray();// 载入XML文件
log.info("正在载入WebService...\r\n ");
StrWebService strWebService = new StrWebService(context);
try {
strWebService.setArray();// 载入WebService数据
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.info("系统初始化配置已完成!");
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class SelectAll {
private static final Logger log = Logger.getLogger(SelectAll.class);
public static ServletContext servletConfig;
public SelectAll() {
public SelectAll(ServletContext context) {
this.servletConfig = context;
}
public List selectAll(String parCde, String parNme, String method) throws SQLException {
List list = new ArrayList<SelectDto>();
ResultSet crs = null;
Connection conn = null;
Statement stmt = null;
String sql = "";
if ("7".equals(method)) {
sql = " ";
}
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(servletConfig);
log.info("got WebApplicationContext "+wac);
DataSource ds=(DataSource)wac.getBean("dataSource");
conn=ds.getConnection();
stmt = conn.createStatement();
crs = stmt.executeQuery(sql);
servletConfig.setAttribute(parNme, list);
return list;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}finally
{
conn.close();
}
return null;
}
public void setArray() throws SQLException {
selectAll("", "province", "7");
}
}
import java.io.File;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
public class LoadXmlFile {
private static final Logger log = Logger.getLogger(LoadXmlFile.class);
public static ServletConfig servletConfig;
public LoadXmlFile() {
}
public LoadXmlFile(ServletConfig context) {
this.servletConfig = context;
}
public void loadXmlFile(String parNme, String method) {
try {
String serviceFile = "";
if ("1".equals(method)) {
serviceFile = servletConfig.getInitParameter("1");
}
if ("2".equals(method)) {
serviceFile = servletConfig.getInitParameter("2");
}
if ("3".equals(method)) {
serviceFile = servletConfig.getInitParameter("3");
}
ServletContext context = servletConfig.getServletContext();
System.out.println("web.xml path="+context.getRealPath("") + serviceFile);
File configFile = new File(context.getRealPath("") + serviceFile);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
context.setAttribute(parNme, builder.parse(configFile));
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
}
}
public void setArray() {
loadXmlFile("edrItems0320Info", "1");
loadXmlFile("riskInfo", "2");//
loadXmlFile("edrItems0331Info", "3");
}
}