title: Set Spring profile in Tomcat web app with no web.xml file
body: <p>I have a webapp that is a RestEASY JAX-RS application and that uses the latest servlet specifications, such as Java EE annotations so that I don't need to create a <code>web.xml</code> file.</p>
  
  <p>The webapp is bundled as <code>foobar.war</code> and dumped into the <code>webapps</code> directory in Tomcat. In fact the same <code>foobar.war</code> is deployed twice in the same Tomcat instance, once as <code>foobar.war</code> and the other as <code>foobar#demo.war</code> (which maps it to <code>foobar/demo</code> as you know).</p>
  
  <p>I configure each mounted webapp by placing <code>conf/Catalina/localhost/foobar.xml</code> and <code>conf/Catalina/localhost/foobar#demo.xml</code> files, that look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context&gt;
    &lt;Environment name="foo" type="java.lang.String" value="bar"/&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>In my JAX-RS application I pull in the value of <code>foo</code> from JNDI using <code>java:comp/env/foo</code>.</p>
  
  <p>So now I added a Java-based Spring configuration named FooBarConfiguration. I load it in my JAX-RS application using <code>new AnnotationConfigApplicationContext(FooBarConfiguration.class)</code>. That all works fine.</p>
  
  <p>So now I've added two profiles to <code>FooBarConfiguration</code>, one named <code>foo</code> and one named <code>bar</code>. But now... how do I tell the webapp which Spring profile to use? (Remember that I have no <code>web.xml</code> file.) Obviously I have to set <code>spring.profiles.active</code> somewhere. But where?</p>
  
  <p>Because the documentation spoke of "environment" and "JNDI", I crossed my fingers and added an environment variable to <code>conf/Catalina/localhost/foobar.xml</code>:</p>
  
  <pre><code>  &lt;Environment name="spring.profiles.active" type="java.lang.String" value="foo"/&gt;
  </code></pre>
  
  <p>No luck.</p>
  
  <p>I can't set a system property, because that will apply to <em>all</em> the webapps, and the idea here is that each <code>foobar.war</code> instance (<code>foobar.war</code> and <code>foobar#demo.war</code>) could each have a different profile specified.</p>
  
  <p>I suppose I could manually pull it out of the Tomcat environment myself, using <code>java:comp/env/spring.profiles.active</code>, but then where do I set the value? (I thought maybe <code>AnnotationConfigApplicationContext</code> would have a constructor where I could set the profile, or at least have a profile setting, but that also seems to be missing.)</p>
  
  <p>(Plus if I'm manually pulling out the setting from JNDI and setting it myself, I might as well switch to the more lightweight Guice and manually load the modules I want. I'm only using the humongous, clunky Spring because it promised to allow easy selection of profiles.)</p>
  
  <p>How can I indicate, external to my WAR file, on a per-webapp basis, which Spring profile I'm using?</p>