Spring 和 filter

标题是 spring和filter,但是这里却是说的spring MVC 项目中需要用到filter,filter中需要用到spring实例化的bean,于是为了简化就形成spring和filter了。

我遇到的问题:项目用的时Spring MVC+Spring,需要一个filter,这个filter中用到了spring中的bean,

但是无论我怎么配置:

  1.使用DelegatingFilterProxy

  2.从WebApplicationContext中获取

      两种方式都得不到spring中实例化的bean,经过两天的摸索,终于明白了:项目中bean的实例化交给了springMVC(也就是DispatcherServlet),并不是通过ContextLoaderListener去实例化的,所以网站所有的bean都是在一个DispatcherServlet中,至于bean在这里的具体位置,我还没有找出来,并没有在ApplicationContent中,这点是让我最郁闷的,不够后来理解了。表达能力有限,直接说解决办法:

把ApplicationContent.xml交给ContextLoaderListener去加载,但是这里面扫描bean的时候一定要把controller中的去掉,XXX-servlet.xml这里面要配置下扫描controller的bean。要工作了,先记录到这里,省得以后遇到这个问题又忘了,记忆力不是很好。

原文地址:https://www.cnblogs.com/andysd/p/3851061.html