`
kanpiaoxue
  • 浏览: 1789827 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

How can I get a Spring bean in a servlet filter?

 
阅读更多

开发基于spring的web程序的时候可能会需要在servlet的filter中得到spring管理的javabean的情况。该如何得到spring管理的javabean呢?

我们应该先得到spring的上下文:ApplicationContext

使用 WebApplicationContextUtils

filter中的代码如下:

public void init(FilterConfig cfg) { 
    ApplicationContext ctx = WebApplicationContextUtils
      .getRequiredWebApplicationContext(cfg.getServletContext());
    this.bean = ctx.getBean(YourBeanType.class);
}

 

参考地址: https://stackoverflow.com/questions/7882042/how-can-i-get-a-spring-bean-in-a-servlet-filter

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics