`
zhanyu1984
  • 浏览: 71138 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

在servlet和filter中获取Spring上下文

 
阅读更多
在servlet中
方法一:在spring上下文加载到内存后直接获取
ApplicationContext applicationContext= WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());  
SystemService  systemService = (SystemService) applicationContext.getBean("systemServiceImpl");

方法二:重新加载资源文件
String dataAccessCfg = "spring/applicationContext.xml ;//资源文件的绝对路径

ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { dataAccessCfg });
BaseDao baseDao = (BaseDao) context.getBean("baseDao");

在filter中:
在filter里面server会给它注入一个FilterConfig对象,在filter里可以使用FilterConfig的filterConfig.getServletContext()方法获取servlet上下文
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics