开发者

Two Spring RequestContextFilter and @Component annotations

I have two filter classes extending from org.springframework.we开发者_高级运维b.filter.RequestContextFilter, both configured in the web.xml in the same way, both using @Component annotation. When I had only one, everything worked correctly. When I added the second one, it didn't get registered.

Why? How can I solve this? I need the two filters?


Since Spring @Component is class based and not name based, the second filter doesn't load because it's being shadowed by the first one. The fact that the two implementation classes are different is irrelevant because Spring searches for something extending Filter.

The solution is to use a qualifier for each @Component (e.g. @Component(value = "MyFilter1") ) and to declare in the web.xml the name of the filter as the qualifier (e.g. <filter><filter-name>MyFilter</filter-name>...</filter>.

Don't forget to use org.springframework.web.filter.DelegatingFilterProxy and declare the filter mapping as well.

Yes - it's strange, but I'm answering my own question immediately. I started writing it after searching for a few hours and then had an idea. I tested it and it works - but there's no reason for someone else to search for a few hours again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜