web启动加载servlet得到Spring bean

本文介绍了一个Java Web应用的初始化过程,包括系统参数加载、XML文件解析及WebService数据配置等关键步骤。通过自定义Servlet实现了Spring上下文的获取与资源初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值