title: Registering a filter in SpringBoot
body: <p>From what I have read, a filter can be registered either by annotating it with <code>@Component</code> or by registering it in a <code>FilterRegistrationBean</code>.  However, I cannot seem to do with just with <code>@Component</code>.</p>
  <p>The following code works</p>
  <pre><code>//@Component
  public class AuthFilter extends OncePerRequestFilter {
  
      @PostConstruct
      public void postConstruct() {
          System.out.println(&quot;*** Loading authFilter&quot;);
      }
      
      @Override
      protected void doFilterInternal(final @NonNull HttpServletRequest httpServletRequest, final @NonNull HttpServletResponse httpServletResponse, final @NonNull FilterChain filterChain) throws ServletException, IOException {
          System.out.println(&quot;*** calling filter&quot;);
          filterChain.doFilter(httpServletRequest, httpServletResponse);
          System.out.println(&quot;*** return from filter&quot;);
      }
  
      @Bean
      public FilterRegistrationBean&lt;AuthFilter&gt; filter() {
          final FilterRegistrationBean&lt;AuthFilter&gt; bean = new FilterRegistrationBean&lt;&gt;();
          bean.setFilter(this);
          return bean;
      }
  }
  </code></pre>
  <p>However, if I comment out the <code>FilterRegistrationBean</code> method and uncomment the <code>@Component</code> annotation, I get the following stack trace at start-up</p>
  <pre><code>{&quot;@timestamp&quot;:&quot;2022-03-21T23:00:52.856Z&quot;,&quot;level&quot;:&quot;WARN&quot;,&quot;message&quot;:&quot;Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat&quot;,&quot;logger_name&quot;:&quot;org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext&quot;,&quot;thread_name&quot;:&quot;main&quot;,&quot;nexusService&quot;:&quot;skeleton-Micro-service&quot;}
  {&quot;@timestamp&quot;:&quot;2022-03-21T23:00:52.868Z&quot;,&quot;level&quot;:&quot;INFO&quot;,&quot;message&quot;:&quot; 
   
  Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.&quot;,&quot;logger_name&quot;:&quot;org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener&quot;,&quot;thread_name&quot;:&quot;main&quot;,&quot;nexusService&quot;:&quot;skeleton-Micro-service&quot;}
  {&quot;@timestamp&quot;:&quot;2022-03-21T23:00:52.897Z&quot;,&quot;level&quot;:&quot;ERROR&quot;,&quot;message&quot;:&quot;Application run failed&quot;,&quot;logger_name&quot;:&quot;org.springframework.boot.SpringApplication&quot;,&quot;thread_name&quot;:&quot;main&quot;,&quot;nexusService&quot;:&quot;skeleton-Micro-service&quot;,&quot;stack_trace&quot;:&quot;org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat 
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) 
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) 
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) 
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) 
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:447) 
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) 
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1356) 
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1345) 
      at com.torchai.service.skeleton.NexusSkeletonServiceApplication.main(NexusSkeletonServiceApplication.java:10) 
  Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat 
      at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:142) 
      at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.&lt;init&gt;(TomcatWebServer.java:104) 
      at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:455) 
      at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:204) 
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:182) 
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160) 
      ... 8 common frames omitted 
  Caused by: java.lang.IllegalStateException: StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[] failed to start 
      at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.rethrowDeferredStartupExceptions(TomcatWebServer.java:187) 
      at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:126) 
      ... 13 common frames omitted 
  &quot;}
  </code></pre>
  <p>I haven't been able to figure out what's wrong.  If I don't need to specify the order, url or any properties on the <code>FilterRegistrationBean</code>, is it still required?</p>
  <h1>Update</h1>
  <p>The filter is in package <code>com.torchai.service.filter;</code> and on my application, class, I have @SpringBootApplication(scanBasePackages = {&quot;com.torchai.service&quot;}).  My @PostConstruct method is getting called.  So the class is definitely getting loaded</p>