在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) ...
你能帮助我吗?
In事实上我不在服务器端使用MVC概念(Spring)。对于我的客户端,我使用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.
所以,这就是我使用Filter bean概念的原因。
So, that is the reason, I use the Filter bean notion.
那么,如何在我的Filter bean中处理会话bean概念?
So, how can I handle my session bean notion in my Filter bean ?
Skaffman,
我实现了你的想法,所以我用以下内容更新了我的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;
}
但是,此方法 handleRequestInternal 永远不会被调用我的网址是: http://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
但我不喜欢硬编码bean的想法我的Java类中的名称。
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屋!