View Full Version : @Resource from servlet filter
Sep 30th, 2009, 05:07 AM
I've got a servlet filter that allow accessing a spring controller only for a couple ip. I defined it in my web.xml:
Inside of it there is a List<String> ipWhiteList.
Now, in my spring's application-context.xml i define:
How can I autowire it?
I tought something similar to @Resource(name ="ipWhiteList") :)
(obviously it isn't so easy!) :(
Note that it isn't a real exigence, I only want to know for future use: autowiring a bean in the context to a servlet out of it. Possible? ;)
Sep 30th, 2009, 07:59 AM
This isn't possible, spring can only (auto)wire/configure beans it knows about. Obviously your filter is not part of the spring configuration or even under the control of spring it is under the control of the servlet container.
You can use a DelegatingFilterProxy and configure your actual filter inside your application context. However if it is for limiting access to a controller it might be better to use a HandlerInterceptor instead.
Sep 30th, 2009, 10:11 AM
Could you please tell me with more details how can I add a handlerInterceptor to my configuration? Can I leave the filter as it is? :o
Sep 30th, 2009, 10:55 AM
But I don't want ALL urls are filtered, only a controller.
Sep 30th, 2009, 01:03 PM
That is why I suggested a HandlerInterceptor.... I suggest the reference guide...
Oct 1st, 2009, 03:29 AM
I read the reference guide at http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#mvc-handlermapping but I don't understand one thing, it isn't explained how to put the interceptor BEFORE the controller, I mean
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
This is what I have, then I implement an IpInterceptor, how can I put it BEFORE the systemInfo.do and NOT BEFORE systemCheck.do? The example in the reference guide does not explain that :rolleyes:
Oct 1st, 2009, 04:12 AM
define multiple HandlerMappings. One with and one without the interceptor.
Oct 1st, 2009, 05:36 AM
Ok, finally... it works! :) Thanks for your help!
Now I'm using two SimpleUrlHandlerMapping with 'order' property 1 for the interceptor-based and 2 for other mappings.
Thank you again!
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.