title: Tomcat/ApplicationContext not able to find out Filter class
body: <blockquote>
    <p>web.xml</p>
  </blockquote>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
        &lt;display-name&gt;Self Service Portal&lt;/display-name&gt;
        &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;home.html&lt;/welcome-file&gt;
          &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
  
        &lt;filter&gt;
          &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.apache.catalina.filters.CorsFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
            &lt;param-name&gt;cors.allowed.methods&lt;/param-name&gt;
            &lt;param-value&gt;GET, POST, PUT, DELETE, OPTIONS, HEAD&lt;/param-value&gt;
          &lt;/init-param&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;
  
        &lt;filter&gt;
          &lt;filter-name&gt;login&lt;/filter-name&gt;
          &lt;filter-class&gt;com.app.api.filter.AuthenticationFilter&lt;/filter-class&gt;
        &lt;/filter&gt;
        &lt;filter-mapping&gt;
          &lt;filter-name&gt;login&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
        &lt;/filter-mapping&gt;
  
        &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
        &lt;/servlet&gt;
        &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
        &lt;/servlet-mapping&gt;
  
         &lt;!-- Spring Context Listener --&gt;
        &lt;listener&gt;
            &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
        &lt;/listener&gt;
  
        &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath*:applicationContext*.xml&lt;/param-value&gt;
        &lt;/context-param&gt;
  
      &lt;/web-app&gt;
  </code></pre>
  
  <blockquote>
    <p>Custom filter class:-</p>
  </blockquote>
  
  <pre><code>package com.app.api.filter;
  
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.annotation.WebFilter;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.http.MediaType;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  @WebFilter("/*")
  public class AuthenticationFilter implements Filter {
      private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilter.class);
  
      public AuthenticationFilter() {
          System.out.println("Authentication - default filter !");
      }
  
      public void destroy() {
          System.out.println("Authentication - destroy !");
      }
  
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
          LOGGER.debug("Request authentication!");
          HttpServletRequest httpRequest = (HttpServletRequest) request;
          String uri = httpRequest.getRequestURI();
          if(uri.equals("/app-api/login") || uri.equals("/app-api/logout")) {
              chain.doFilter(request, response);
              return;
          }
  
          HttpSession session = httpRequest.getSession(false);
          if(session == null || session.getAttribute("user") == null) {
              writeInvalidCredentialResponse((HttpServletResponse) response);
          } else {
              chain.doFilter(request, response);
          }
      }
  
      private void writeInvalidCredentialResponse(HttpServletResponse response) throws IOException {
          Map&lt;String, String&gt; errorResponse = new HashMap&lt;String, String&gt;();
          errorResponse.put("message", "Please login with right credentials!");
          ObjectMapper mapper = new ObjectMapper();
          String responseMessage = mapper.writeValueAsString(errorResponse);
          LOGGER.debug("Invalid request authentication!");
          LOGGER.debug(responseMessage);
          response.getWriter().write(responseMessage);
          response.setContentType(MediaType.APPLICATION_JSON_VALUE);
          response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
      }
  
      public void init(FilterConfig fConfig) throws ServletException {
          System.out.println("Authentication - init !");
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>Project hierarchy:-</p>
    
    <blockquote>
      <p>A - parent maven module</p>
      
      <p>B - RESTful webservices sub module (contains - /WEB-INF/web.xml, /WEB-INF/dispatcher-servlet.xml)</p>
      
      <p>C - persistence layer sub module (contains - /resources/applicationContext.xml, /resources/persistence.xml)</p>
    </blockquote>
  </blockquote>
  
  <p>Please help me to resolve this issue to make my both spring context up and running (ui - webservices and persistence)
  Many thanks in advance !</p>
  
  <blockquote>
    <p>app-api
    /app-api/src/main/webapp/WEB-INF/dispatcher-servlet.xml</p>
  </blockquote>
  
  <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:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;     
      &lt;import resource="classpath:/applicationContext-data.xml" /&gt;
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.app" /&gt;
      &lt;context:property-placeholder location="classpath:*.properties" /&gt;
      &lt;mvc:annotation-driven/&gt;    
  &lt;/beans&gt;
  </code></pre>
  
  <blockquote>
    <p>app-data
    /app-data/src/main/resources/applicationContext-data.xml</p>
  </blockquote>
  
  <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:context="http://www.springframework.org/schema/context"
      xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:p="http://www.springframework.org/schema/p" 
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.0.xsd 
      http://www.springframework.org/schema/jdbc 
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 
      http://www.springframework.org/schema/data/jpa 
      http://www.springframework.org/schema/data/jpa/spring-jpa-2.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
      &lt;!-- ************* JPA/Hibernate configuration ************* --&gt;
      &lt;bean 
          name="entityManagerFactory" 
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="com.app.data.*" /&gt;
          &lt;property name="persistenceXmlLocation" value="classpath:persistence.xml" /&gt;
          &lt;property name="persistenceUnitName" value="persistence-unit" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean 
          id="transactionManager" 
          class="org.springframework.orm.jpa.JpaTransactionManager" 
          p:entityManagerFactory-ref="entityManagerFactory" /&gt;
  
      &lt;bean 
          id="dataSource" 
          class="org.apache.commons.dbcp.BasicDataSource"
          p:driverClassName="com.mysql.jdbc.Driver" 
          p:url="jdbc:mysql://localhost:3306/app"
          p:username="root" 
          p:password="root123" 
          p:initialSize="5"
          p:maxActive="10"&gt;
      &lt;/bean&gt;
  &lt;context:component-scan base-package="com.app.data.*" /&gt;
      &lt;tx:annotation-driven /&gt;
  &lt;/beans&gt;    
  </code></pre>