在 Filter bean 类中使用一些 bean? [英] Using some beans in Filter bean class?
问题描述
在我的过滤器 bean 类中,我添加了一些 bean 依赖项(带有 @Autowired
注释).但是在方法 doFilter()
中,我所有的依赖 bean 都为 null ...
In my filter bean class, I added some beans dependency (with @Autowired
annotation). But in the method doFilter()
, all my dependency beans have null ...
public class FacebookOAuth implements Filter
{
@Autowired
private BusinessLogger logger;
@Autowired
private IUserSessionInfo userSessionInfo;
@Autowired
private FacebookOAuthHelper oAuthHelper;
public void init(FilterConfig fc) throws ServletException
{
// Nothing to do
}
public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException
{
// HttpServletRequest req = (HttpServletRequest)sr;
HttpServletResponse res = (HttpServletResponse) sr1;
String code = sr.getParameter("code");
if (StringUtil.isNotBlankStr(code))
{
String authURL = this.oAuthHelper.getAuthURL(code);
this.oAuthHelper 等于 null(和其他依赖 bean)...
this.oAuthHelper is equal at null (and other dependancy beans to) ...
你能帮我吗?
事实上,我在服务器端 (Spring) 上没有使用 MVC 概念.对于我的侧客户端,我使用 Flex 技术和 BlazeDS servlet 与我的服务器进行通信.
In fact I don't use MVC notion on server side (Spring). For my side client I use Flex technology and BlazeDS servlet ton communicate with my server.
所以,这就是我使用过滤器 bean 概念的原因.
So, that is the reason, I use the Filter bean notion.
那么,如何在我的过滤器 bean 中处理我的会话 bean 概念?
So, how can I handle my session bean notion in my Filter bean ?
斯卡夫曼
我实现了你的想法,所以我更新了我的 application.xml :
I implemented your idea, so I update my application.xml with :
<bean id="FacebookOAuthHandler" class="com.xx.FacebookOAuthHandler" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/fbauth">FacebookOAuthHandler</prop>
</props>
</property>
</bean>
和我的 FacebookOAuthHandler 类:
public class FacebookOAuthHandler extends AbstractController
{
@Autowired
private BusinessLogger logger;
@Autowired
private IUserSessionInfo userSessionInfo;
@Autowired
private FacebookOAuthHelper oAuthHelper;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO
return null;
}
但是,当我的 URL 是:时,永远不会调用此方法 handleRequestInternalhttp://xx.xx.xx.xx/MyApp/fbauth
But, this method handleRequestInternal is never called when my URL is : http://xx.xx.xx.xx/MyApp/fbauth
推荐答案
我遇到了同样的问题,我的第一个想法是手动强制 Spring 将 @Autowired 注释应用到过滤器,就像这里建议的那样
I was facing the same problem and my first idea was to manually force Spring to apply @Autowired annotation to the filter like proposed here
http://forum.springsource.org/showthread.php?60983-Autowiring-the-servlet-filter
但我不喜欢在我的 Java 类中硬编码 bean 名称的想法.
But I don't like the idea of hardcoding the bean name in my Java class.
我找到了一种更简洁的方法:
I found an cleaner way that works as well:
public void init(FilterConfig filterConfig) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
filterConfig.getServletContext());
}
这篇关于在 Filter bean 类中使用一些 bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!