【发布时间】:2014-12-25 10:33:38
【问题描述】:
我正在开发 spring-mvc 应用程序。
我无法访问过滤器中的 bean。我遇到了异常
org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.abc.app.SessionValue] is defined
我去 throw https://stackoverflow.com/a/11709272/3898076,但找不到问题。
我的 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>
并且 spring_xyz-servlet.xml 包含组件扫描条目。
<context:component-scan base-package="com.abc.app" />
<context:annotation-config />
<context:spring-configured />
过滤代码:
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(fConfig.getServletContext());
springContext.getBean(SessionValue.class);
这里面有配置问题吗?
谢谢。
【问题讨论】:
-
SessionValue在哪里注册为 Spring bean? -
抱歉注册为使用注解的组件。
-
WebApplicationContextUtils将检索根上下文,即由ContextLoaderListener加载的上下文,它不会访问由DispatcherServlet加载的上下文。将 bean 移动到根上下文。 -
我还没有创建applicationContext.xml,我已经完成了spring_xyz-servlet.xml中的所有条目。所以在这种情况下,我是否必须创建 applicationContext.xml。或者我应该在
中定义 spring_xyz-servlet.xml 吗? -
您真的需要过滤器还是有其他方法可以实现您想要实现的目标?就像来自 Spring 的
HandlerInterceptor?还为ContextLoaderListener定义相同的配置文件,因为这会复制所有的bean,通常不是你真正想要的。
标签: java spring spring-mvc filter