title: Spring Boot YML and StandAlone Tomcat 8 Server
body: <p>I have the following directory structure/config file:</p>
  
  <pre><code>src/main/resource/config: 
  application.yml 
  application-dev.yml 
  application-sit.yml
  </code></pre>
  
  <p>Note according to the "<strong>Bootiful Configuration</strong>" <a href="https://spring.io/blog/2015/01/13/configuring-it-all-out-or-12-factor-app-style-configuration-with-spring" rel="noreferrer">https://spring.io/blog/2015/01/13/configuring-it-all-out-or-12-factor-app-style-configuration-with-spring</a>:</p>
  
  <blockquote>
    <p>Spring Boot will read the
    properties in src/main/resources/application.properties by default. If
    a profile is active, it will also automatically reads in the
    configuration files based on the profile name, like
    src/main/resources/application-foo.properties where foo is the current
    profile. If the Snake YML library is on the classpath, then it will
    also automatically load YML files.</p>
  </blockquote>
  
  <p>Since snake YML jar is in class path if I set <code>--spring.profiles.active=dev</code> as a
  program arg in eclipse run configuration and use this as my main method Ever thing works as expected:</p>
  
  <pre><code>  public static void main(String[] args) {
          SpringApplication app = new SpringApplication(Application.class);
  
          SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
  
          // Check if the selected profile has been set as argument.
          // if not the development profile will be added
          addDefaultProfile(app, source);
  
          app.run(args);
      }
  
      /**
       * Set a default profile if it has not been set
       */
      private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) {
          if (!source.containsProperty("spring.profiles.active")) {
              app.setAdditionalProfiles(Constants.SPRING_PROFILE_DEVELOPMENT);
          }
      }
  </code></pre>
  
  <p>(Please note the main method reference above is from the following class used in my code: <a href="https://github.com/jarias/generator-jhipster-ember/blob/master/app/templates/src/main/java/package/_Application.java" rel="noreferrer">https://github.com/jarias/generator-jhipster-ember/blob/master/app/templates/src/main/java/package/_Application.java</a>)</p>
  
  <p>Everything works as expected for spring.profile.active=dev. Which means that both:
  <strong>application.yml</strong>(loaded by default) and <strong>application-dev.yml</strong>(active profile) property files are loaded and <strong>excludes</strong> <em>application-sit.yml</em> since sit isn't an active profile.</p>
  
  <p>This embedded container works great for dev testing. However I want to release this into production by generating a war and deploy it to a standalone Tomcat8 Server. </p>
  
  <p>For that I created an implementation of WebApplicationInitializer which is required by Tomcat8 server to automatically detect, bootstrap and start spring application on the standalone server.</p>
  
  <pre><code>@Configuration
  public class WebAppInit implements WebApplicationInitializer {
  
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
          }
  }
  </code></pre>
  
  <p>After deploying the war I receive the following error I attempt to start the standalone server and receive the following error :</p>
  
  <blockquote>
    <p>Caused by: org.springframework.beans.factory.enter code
    hereBeanCreationException: Could not autowire field: private
    java.lang.String com.titlefeed.config.db.DbConfigJPA.databaseUrl;
    nested exception is java.lang.IllegalArgumentException: Could not
    resolve placeholder spring.data.postgres.uri' in string value
    "${spring.data.postgres.uri}"</p>
  </blockquote>
  
  <p>Which implies the Tomcat Server/Spring <strong>isnt</strong> loading the application-dev.yml since that contains the properties: <strong>spring.data.postgres.uri</strong></p>
  
  <p>So I attempted the following two solutions</p>
  
  <ol>
  <li>added <code>-Dspring.profiles.active=dev</code> to JAVA_OPTS in tomcat/bin/catalina.sh</li>
  <li>added <code>spring.profiles.active=dev</code> to tomcat/conf/catalina.properties</li>
  </ol>
  
  <p>And neither of them worked. How can I get the standalone tomcat server to load the yml file associated with the spring.profiles.active property.</p>
  
  <p>It works fine for the embedded springboot server started from eclipse but doesnt for an standalong server ?</p>
  
  <p><strong>EDIT1: M. Deinum</strong>  - Implemented your suggested solution below however still got the following error:</p>
  
  <p><code>Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.postgres.uri' in string value "${spring.data.postgres.uri}</code></p>
  
  <p>It seems like the -Dspring.profiles.active=dev isn't getting set.</p>
  
  <pre><code>@Configuration
  public class WebAppInit extends SpringBootServletInitializer {
  
   @Override
  
      protected WebApplicationContext createRootApplicationContext(
              ServletContext servletContext) {
             log.info("Properly INITALIZE spring CONTEXT");
             ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
             return super.createRootApplicationContext(servletContext);
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT 2 ACV:</strong> - Adding "--spring.profiles.active=dev" as apart of JAVA_OPTS variable in the startup script: tomcat/bin/catalina.sh is not a viable option</p>
  
  <p>E.g:</p>
  
  <pre><code> JAVA_OPTS="$JAVA_OPTS --spring.profiles.active=dev ...etc
  </code></pre>
  
  <p>Gives the following error:</p>
  
  <blockquote>
    <p>Unrecognized option: --spring.profiles.active=dev Error: Could not
    create the Java Virtual Machine."</p>
  </blockquote>
  
  <p><strong>EDIT 3:</strong>
  Amended <strong>application.yml</strong> to include the following property</p>
  
  <pre><code>spring:
    profiles:
      active: dev
  </code></pre>
  
  <p>Redeployed the war. Went to the exploded tomcat directory location to ensure the property was present <strong>webapps/feedserver/WEB-INF/classes/config/application.yml</strong></p>
  
  <p>And the issue still occurred.</p>
  
  <p><strong>EDIT 4:</strong> Added application.properties under the tomcat exploded webdir: webapps/feedserver/WEB-INF/classes/application.properties:</p>
  
  <pre><code>spring.profiles.active=dev
  spring.data.postgres.uri=jdbc:postgresql://localhost:5432/feedserver
  </code></pre>
  
  <p>restarted tomcat and the issue still occurred.</p>
  
  <p>Its seems like its not picking up either <strong>application.properties</strong> or <strong>application.yml</strong></p>
  
  <p><strong>EDIT 5</strong> Used the recommended way to start the spring boot server for an external container:</p>
  
  <pre><code>@Configuration
  public class WebAppInit extends SpringBootServletInitializer {
  
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
       return application.sources(Application.class);
   }
  
  }
  </code></pre>
  
  <p><strong>Edit 6:</strong></p>
  
  <p>I added <strong>-Dspring.profiles.active=dev</strong> to the start command args:</p>
  
  <pre><code>/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java -Djava.util.logging.config.file=/Users/shivamsinha/Desktop/Programming/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dlog4j.rootLevel=ERROR -Dlog4j.rootAppender=console -DENV=dev -Dlog4j.configuration=/WEB-INF/classes/properties/log4j.properties -DTOMCAT_DIR=WEB-INF/classes/ -Djava.endorsed.dirs=/Users/shivamsinha/Desktop/Programming/tomcat/endorsed -classpath /Users/shivamsinha/Desktop/Programming/tomcat/bin/bootstrap.jar:/Users/shivamsinha/Desktop/Programming/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/Users/shivamsinha/Desktop/Programming/tomcat -Dcatalina.home=/Users/shivamsinha/Desktop/Programming/tomcat -Djava.io.tmpdir=/Users/shivamsinha/Desktop/Programming/tomcat/temp org.apache.catalina.startup.Bootstrap -Dspring.profiles.active=dev start
  </code></pre>
  
  <p>However I stil get the following exception in the logs:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.titlefeed.config.db.DbConfigJPA.databaseUrl; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.postgres.uri' in string value "${spring.data.postgres.uri}"
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      ... 68 more
  Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.postgres.uri' in string value "${spring.data.postgres.uri}"
      at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
      at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
      at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
      at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
      at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
      at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
      ... 70 more
  
  02-Sep-2015 03:15:40.472 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive /Users/shivamsinha/Desktop/Programming/tomcat/webapps/feedserver-1.0.0.war
   java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/feedserver-1.0.0]]
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:728)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
      at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:917)
      at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1701)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      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)
  </code></pre>