现在的位置: 首页 > 综合 > 正文

web项目中通过spring获得ApplicationContext

2013年03月29日 ⁄ 综合 ⁄ 共 602字 ⁄ 字号 评论关闭

方法一、

@Override
public void sessionDestroyed(HttpSessionEvent event) {

ServletContext context
= event.getSession().getServletContext();
ApplicationContext ctx
= WebApplicationContextUtils.getWebApplicationContext(context);
LoginTempService loginTempService
= (loginTempService)ctx.getBean("loginTempService");
}

方法二、如果在 web工程里,有几个方法,我觉得这个方法最方便。建个类,实现spring的ApplicationContextAware 接口,即 

public class SpringContextTool implements ApplicationContextAware {
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext acx) {
context
= acx;
}

public static ApplicationContext getApplicationContext() {
return context;
}
}

抱歉!评论已关闭.