title: Exception instantiating filter in Spring MVC web-app
body: <p>I need to enable <em>cross-origin resource sharing</em> in my RESTful service implemented using Spring MVC. The service works correctly, but now I need to access resources from another domain.</p>
  
  <p>I found some posts suggesting to add a filter in my web-app. So I added this parts:</p>
  
  <p><strong>Filter class</strong></p>
  
  <pre><code>package org.canvassservice.filters;
  
  import java.io.IOException;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.springframework.web.filter.OncePerRequestFilter;
  
  public class CorsFilter extends OncePerRequestFilter {
  
      @Override
      protected void doFilterInternal(HttpServletRequest request,
              HttpServletResponse response, FilterChain filterChain)
              throws ServletException, IOException {
          response.addHeader("Access-Control-Allow-Origin", "*");
          if (request.getHeader("Access-Control-Request-Method") != null
                  &amp;&amp; "OPTIONS".equals(request.getMethod())) {
              // CORS "pre-flight" request
              response.addHeader("Access-Control-Allow-Methods",
                      "GET, POST, PUT, DELETE");
              response.addHeader("Access-Control-Allow-Headers",
                      "X-Requested-With,Origin,Content-Type, Accept");
          }
          filterChain.doFilter(request, response);
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>In web.xml</strong></p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;corsFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.OncePerRequestFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;corsFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>application.context</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;context:component-scan base-package="org.canvassservice.controllers" /&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean id="corsFilter" class="org.canvassservice.filters.CorsFilter" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>But when I run my application, I get this exception (in Tomcat log):</p>
  
  <pre><code>GRAVE: Exception starting filter corsFilter java.lang.InstantiationException
      at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at java.lang.Class.newInstance0(Class.java:355)
      at java.lang.Class.newInstance(Class.java:308)
      at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
      at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
      at org.apache.catalina.core.ApplicationFilterConfig.&lt;init&gt;(ApplicationFilterConfig.java:115)
      at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4071)
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:4725)
      at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
      at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
      at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
      at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
      at org.apache.catalina.core.StandardService.start(StandardService.java:525)
      at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
  </code></pre>
  
  <p>With no clear cause explained. Any idea about the problem?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried using <code>DelegatingFilterProxy</code> instead of <code>OncePerRequestFilter</code>, declaring the filter bean in <code>application.context</code>, but a new exception is thrown:</p>
  
  <pre><code>GRAVE: Exception starting filter corsFilter
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'corsFilter' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
      at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:323)
      at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
      at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
      at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
      at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
      at org.apache.catalina.core.ApplicationFilterConfig.&lt;init&gt;(ApplicationFilterConfig.java:115)
      at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4071)
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:4725)
      at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
      at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
      at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
      at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
      at org.apache.catalina.core.StandardService.start(StandardService.java:525)
      at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
  </code></pre>