如何加载ApplictionContext

类别:Java 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it11708.htm