Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
如何加载ApplictionContext

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

web.xml中:
   <servlet>
        <servlet-name>initContext</servlet-name>
        <servlet-class>com.it168.control.InitContext</servlet-class>
        <load-on-startup>9999</load-on-startup>
    </servlet> 
使用9999表示此Servlet的Load顺序在其它Servlet之后。
InitContext.java:
/*
 * Created on 2004-11-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.it168.control;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import com.it168.Constants;
import com.it168.common.ApplicationException;
import com.it168.common.HibernateUtil;
import com.it168.common.It168Context;

/**
 * @author iterator99
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class InitContext extends HttpServlet {

 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.GenericServlet#init()
  */
 private transient Logger logger = Logger.getLogger(this.getClass());
 public void init() throws ServletException {
  logger.info("InitContext it168 ");
  try {
   It168Context
     .setAppContext((ApplicationContext) this
       .getServletContext()
       .getAttribute(
         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
  } catch (Exception e) {
   e.printStackTrace();
  }

  try {
   HibernateUtil.init();
  } catch (ApplicationException ae) {
   ae.printStackTrace();
  }
  String propertiesfilename = getServletContext().getRealPath("/")
    + Constants.APP_FILEPATH;
//  try {
//   SmsListener.run(propertiesfilename);
//  } catch (ApplicationException ae) {
//   ae.printStackTrace();
//  }
  super.init();
 }
}

It168Context.java:
/*
 * Created on 2004-11-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.it168.common;

import org.springframework.context.ApplicationContext;

/**
 * @author iterator99
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class It168Context {
 private static ApplicationContext appContext = null;
 /**
  * @return Returns the appContext.
  */
 public static ApplicationContext getAppContext() {
  return appContext;
 }
 /**
  * @param appContext
  *            The appContext to set.
  */
 public static void setAppContext(ApplicationContext appContext) {
  It168Context.appContext = appContext;
 }
 public static Object getBean(String id) {
  Object object = null;
  if (appContext == null) {
   return null;
  }
  object = appContext.getBean(id);
  return object;
 }
 public static Object getBean(String id, Class clz) {
  Object object = null;
  if (appContext == null) {
   return null;
  }
  object = appContext.getBean(id, clz);
  return object;
 }
}

使用例:
  PartnerBiz partnerBiz = (PartnerBiz) It168Context
  .getBean(Constants.partnerBiz);




相关文章

相关软件