如何加载ApplictionContext

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航