title: embedded tomcat valve spring boot
body: <p>I'm trying to configure the <code>LogbackValve</code> for getting access logs in case my Spring Boot based web application is running from embedded Tomcat. Following is the code for configuration:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>import javax.servlet.Servlet;
  
  import org.apache.catalina.startup.Tomcat;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
  import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  import ch.qos.logback.access.tomcat.LogbackValve;
  
  @Configuration
  public class EmbeddedTomcatConfigurator {
  
      @Bean
      @ConditionalOnClass({ Servlet.class, Tomcat.class })
      @ConditionalOnBean(value = LogbackValve.class)
      public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(LogbackValve logbackValve) {
          TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
          factory.addContextValves(logbackValve);
          return factory;
      }
  
      @Bean
      @ConditionalOnProperty(name = "embedded.tomcat.logback.access.config.path")
      public LogbackValve logbackValve(@Value("${embedded.tomcat.logback.access.config.path:}") String fileName) {
          LogbackValve logbackValve = new LogbackValve();
          logbackValve.setFilename(fileName);
          return logbackValve;
      }
  }
  </code></pre>
  
  <p>However, everytime I start the application using "mvn spring-boot:run" in debug mode, I see logs saying, "LogbackValve not found" when trying to create instance of "tomcatEmbeddedServletContainerFactory" bean. However, another log statement indicates creation of this bean. Due to this, it always initializes the bean defined in the auto-configuration class "org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration".</p>
  
  <p>For now, I've modified my class as :</p>
  
  <pre class="lang-java prettyprint-override"><code>import javax.servlet.Servlet;
  
  import org.apache.catalina.startup.Tomcat;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  import ch.qos.logback.access.tomcat.LogbackValve;
  
  @Configuration
  public class EmbeddedTomcatConfigurator {
  
      @Bean
      @ConditionalOnClass({ Servlet.class, Tomcat.class })
      @ConditionalOnProperty(name = "embedded.tomcat.logback.access.config.path")
      public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(@Value("${embedded.tomcat.logback.access.config.path:}") String logbackAccessPath) {
          TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
          factory.addContextValves(getLogbackValve(logbackAccessPath));
          return factory;
      }
  
      private LogbackValve getLogbackValve(String fileName) {
          LogbackValve logbackValve = new LogbackValve();
          logbackValve.setFilename(fileName);
          return logbackValve;
      }
  
  }
  </code></pre>
  
  <p>I've already asked this question on <a href="https://github.com/spring-projects/spring-boot/issues/2937" rel="nofollow">Git</a> and it has been resolved. But, here, the point I'm trying to bring up is, why the @ConditionalOnBean(value = LogbackValve.class) isn't detecting the bean, which has been defined as well.</p>