錛屽茍鎶婅繖浜涘弬鏁版斁緗埌application閲岋紝jsp欏甸潰鍙互浠庝腑鍙栧嚭銆?br>
1.鍦╳eb.xml涓厤緗細
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Dispatcher-
servlet.xml,/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>context</servlet-name>
<servlet-
class>org.springframework.web.context.ContextLoaderServlet</servlet-
class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>InitialServlet</servlet-name>
<servlet-
class>com.anylinks.billreturn.Web.InitialServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
2.servlet浠g爜
package com.anylinks.billreturn.Web;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import
org.springframework.web.context.support.WebApplicationContextUtils;
import com.anylinks.billreturn.BO.SysParameter;
import com.anylinks.billreturn.Service.ISysParameterService;
/*
* 鍒濆鍖朣ervlet錛屼粠鏁版嵁搴撲腑璇誨彇鍙傛暟琛紝淇濆瓨鍦╝pplication閲?br> * @author 钄$
* 鍒涘緩鏃ユ湡錛?006-1-9
*/
public class InitialServlet extends HttpServlet {
private Log log = LogFactory.getLog(this.getClass());
private ISysParameterService sysParameterService;
/**
* 浠庢暟鎹簱涓鍙栧弬鏁拌〃錛屼繚瀛樺湪application閲?br> *
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
log.debug("start to intitail ");
// 鑾峰彇WebApplicationContext
ServletContext application = getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getWebApplicationContext
(application);
// 璋冪敤sysParameterService鍙栧嚭鎵鏈夌殑緋葷粺鍙傛暟
sysParameterService = (ISysParameterService) wac
.getBean("sysParameterService");
Collection paras =
sysParameterService.findAllParameters();
log.debug("sys parameters size:" + paras.size());
// 鎶婂弬鏁板姞鍒癮pplication閲屽幓
for (Iterator iter = paras.iterator(); iter.hasNext
();) {
SysParameter para = (SysParameter) iter.next
();
application.setAttribute(para.getParaName(),
para.getParaValue());
log.debug("initial parameter: key=" +
para.getParaName()
+ ", value=" +
para.getParaValue());
}
}
}
闇瑕佹敞鎰忕殑鍦版柟錛?br>1.浠呬粎閰嶇疆涓涓狣ispatcherServlet鏄笉澶熺殑錛屾垜寮濮嬪氨鏄繖鏍鳳紝鐒跺悗鍐峴ervlet
閲岄潰鎬庝箞鍙栭兘鍙栦笉鍒癢ebApplicationContext 銆傞厤緗笂
org.springframework.web.context.ContextLoaderServlet涔嬪悗鎵嶈兘鍙栫殑鍒?br>
WebApplicationContext 銆?br>2.娉ㄦ剰涓涓?lt;load-on-startup>3</load-on-startup>錛屽洜涓虹敤鍒皊pring鐨?br>
hibernateDaoSupport,鎵浠ュ繀欏誨湪spring鍔犺澆瀹屼箣鍚庡啀鍔犺澆InitialServlet.

]]>