0

I am developing spring-mvc application.

I am not able to access beans in my filter. I am getting below exception

 org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.abc.app.SessionValue] is defined

I went throw https://stackoverflow.com/a/11709272/3898076, but not able to find the problem.

I have below entry in my web.xml

<servlet>
    <servlet-name>myapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring_xyz-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

And spring_xyz-servlet.xml contains component-scan entry.

<context:component-scan base-package="com.abc.app" />
<context:annotation-config />
<context:spring-configured />

Filter code:

WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(fConfig.getServletContext());
springContext.getBean(SessionValue.class);

Is there any configuration issue in this?

Thanks.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • And where is `SessionValue` registered as a Spring bean? – Predrag Maric Dec 25 '14 at 10:41
  • Sorry it is registered as Component using annotation. – Naman Gala Dec 25 '14 at 10:43
  • 1
    The `WebApplicationContextUtils` will retrieve the root context, that is the one loaded by the `ContextLoaderListener` it doesn't access the one loaded by the `DispatcherServlet`. Move the bean to the root context. – M. Deinum Dec 25 '14 at 11:11
  • I haven't created applicationContext.xml, I have done all the entries in spring_xyz-servlet.xml. So in this case, Do I have to create applicationContext.xml. Or should I define spring_xyz-servlet.xml in ? – Naman Gala Dec 25 '14 at 11:16
  • 1
    Do you really need the filter or is there another way to achieve what you want to achieve? Like a `HandlerInterceptor` from Spring? Also defining the same config file for the `ContextLoaderListener` as that would duplicate all your beans, generally not something you really want. – M. Deinum Dec 26 '14 at 10:06
  • Instead of defining twice, I can go for HandlerInterceptor. Thanks – Naman Gala Dec 29 '14 at 05:02

1 Answers1

1

If there is no other constraints you should try to use Spring MVC Handler Interceptors, because you are in Spring context, and handlers are almost similar to Filters.

http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html

Example:

http://www.journaldev.com/2676/spring-mvc-interceptors-example-handlerinterceptor-and-handlerinterceptoradapter

mommcilo
  • 956
  • 11
  • 28
  • Yes, that can be used. But I want to run using filter also. – Naman Gala Dec 25 '14 at 12:49
  • I checked your error better and it looks you already have Spring context there. Do you annotate your SessionValue with Component. Or if this is interface have you annotated implementation class with Component or Service? – mommcilo Dec 25 '14 at 12:57
  • Yes, @M.Deinum pointed out the problem. Just finding the solution how to do that in my application. – Naman Gala Dec 25 '14 at 13:00