title: Access spring environment in a separate servlet
body: <p>I have to port an old spring app with servlets to a spring boot app. </p>
  
  <p>I have configured a java style configuration file for existing servlets but I am unable to pass the spring environment to them.</p>
  
  <p>for example the java config looks like this (I have skipped the real mapping)</p>
  
  <pre><code>@Configuration
  public class ServletConfig {
  
      @Bean
      public ServletRegistrationBean initServletRegistration() throws ServletException {
          ServletRegistrationBean bean = new ServletRegistrationBean();
          try {
              bean.setServlet(new InitServlet());
          } catch (Exception e) {
              NewLog.logError(this, "InitServlet threw an exception: " + e, e);
              throw new ServletException("InitServlet threw an exception: " + e);
          }
          bean.setUrlMappings(Arrays.asList("...."));
          bean.setLoadOnStartup(1);
          return bean;
      }
  
      @Bean
      public ServletRegistrationBean testReceiveServiceServletRegistration() {
          ServletRegistrationBean bean = new ServletRegistrationBean();
          bean.setServlet(new TestReceiveServiceServlet());
          bean.setUrlMappings(Arrays.asList("...."));
          return bean;
      }
  
      @Bean
      public ServletRegistrationBean insertIntoInputQueueServletRegistration() {
          ServletRegistrationBean bean = new ServletRegistrationBean();
          bean.setServlet(new InsertIntoInputQueueServlet());
          bean.setUrlMappings(Arrays.asList("...."));
          return bean;
      }
  
      @Bean
      public ServletRegistrationBean nonCxfRedirectServletRegistration() {
          ServletRegistrationBean bean = new ServletRegistrationBean();
          bean.setServlet(new NonCxfRedirectServlet());
          bean.setUrlMappings(Arrays.asList(".....");
          return bean;
      }
  </code></pre>
  
  <p>then in the servlet I would like to inject the spring environment but I always get null for the env vairable</p>
  
  <pre><code>public class InitServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      @Autowired
      Environment env;
  
      public InitServlet() throws Exception {
      }
  ........
  }
  </code></pre>
  
  <p>The servlets itself works ok If i debug them on the given url mappings they respond, I would just like to access the spring environment from them to get the data from application-X.properties depending on the given spring profile.</p>