DelegatingFilterProxy 中的 NoSuchBeanDefinitionException
Posted
技术标签:
【中文标题】DelegatingFilterProxy 中的 NoSuchBeanDefinitionException【英文标题】:NoSuchBeanDefinitionException in DelegatingFilterProxy 【发布时间】:2016-12-27 14:41:47 【问题描述】:我正在使用 spring mvc 开发 Web 应用程序。我想将 spring bean 注入到我的 Servlet 过滤器中。
我参考了这个教程http://www.deadcoderising.com/2015-05-04-dependency-injection-into-filters-using-delegatingfilterproxy/
以下是我的文件列表
@Component("auditFilter")
public class AuditFilter implements Filter
@Autowired
private AuditHandler auditHandler;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
System.out.println("do filter");
auditHandler.auditRequest(req);
chain.doFilter(req, res);
public void init(FilterConfig filterConfig) throws ServletException
public void destroy()
@Component("auditHandler")
public class AuditHandler
public void auditRequest(ServletRequest request)
System.out.println("Received request from " + request.getRemoteAddr());
@Controller
public class HomeController
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model)
logger.info("Welcome home! The client locale is .", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.xyz.springtest" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the $webappRoot/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>auditFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>appName</param-name>
<param-value>di-example</param-value>
</init-param>
</filter>
</web-app>
当我启动我的应用程序时,我得到以下异常
SEVERE: Exception starting filter auditFilter
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'auditFilter' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)
at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236)
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我错过了什么?
以下帖子对我没有帮助
@Autowired in DelegatingFilterProxy
How can I get a Spring bean in a servlet filter?
Spring Framework filter, bean not injected
Spring and @Autowired on a DelegatingFilterProxy
【问题讨论】:
你确定你的过滤器类在com.xyz.springtest
包下吗?
@MaciejMarczuk 是的,是的
【参考方案1】:
你必须在'root-context.xml'中加载你的bean,它是加载应用程序上下文bean的地方。
你的 'servlet-context.xml' 加载 WebApplicationContext,所以在这里定义你的 mvc 控制器,如果你想访问应用程序范围的其他 bean,应该移动到你的根应用程序上下文。您的应用程序有一个根上下文,但可以有许多应用程序上下文(对于每个调度程序 servlet。)。 spring-servlet.xml 中的 bean 可以引用根应用上下文中的 bean,反之则不行
在'root-context.xml'中定义你的bean
【讨论】:
真的吗?以上是关于DelegatingFilterProxy 中的 NoSuchBeanDefinitionException的主要内容,如果未能解决你的问题,请参考以下文章
DelegatingFilterProxy 的独立 Spring 上下文
spring DelegatingFilterProxy 过滤器 的原理及运用
DelegatingFilterProxy(委派过滤器代理类)使用
ClassNotFoundException DelegatingFilterProxy
DelegatingFilterProxy 在没有 Spring Security 的 Spring MVC 应用程序上调用了两次