title: Spring: Cannot autowire beans from parent context
body: <p>I have a Spring Boot (1.4.0) application, which, during initialization, starts a 2nd context (I need that because I have to publish a web service using a specific kind of authorization while the parent context publishes a different service).</p>
  
  <p>I created a child context like so:</p>
  
  <pre><code>@Configuration
  @ConditionalOnClass({Servlet.class, DispatcherServlet.class})
  @ConditionalOnWebApplication
  public class ChildContextConfiguration implements ApplicationContextAware, ApplicationListener&lt;ContextRefreshedEvent&gt; {
  
      private final Logger logger = LoggerFactory.getLogger(ChildContextConfiguration.class);
      private ApplicationContext applicationContext;
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          this.applicationContext = applicationContext;
      }
  
      private void createChildContext() {
          final AnnotationConfigEmbeddedWebApplicationContext childContext = new AnnotationConfigEmbeddedWebApplicationContext(ChildConfiguration.class);
          childContext.setParent(this.applicationContext);
          childContext.setId(this.applicationContext.getId() + ":child");
      }
  
      @Override
      public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
          logger.info("creating child context");
          createChildContext();
      }
  }
  </code></pre>
  
  <p>The child context's configuration class looks like this:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = {"com.example.child"})
  @PropertySource("file:some-config.properties")
  @ConfigurationProperties(prefix = "child")
  @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
  public class ChildConfiguration {
  
      private Integer port;
      private String keyStore;
      private String keyStorePass;
      private String keyPass;
      private String trustStore;
      private String trustStorePass;
      private String packageBase;
  
      public void setPort(Integer port) {
          this.port = port;
      }
  
      public void setKeyStore(String keyStore) {
          this.keyStore = keyStore;
      }
  
      public void setKeyStorePass(String keyStorePass) {
          this.keyStorePass = keyStorePass;
      }
  
      public void setKeyPass(String keyPass) {
          this.keyPass = keyPass;
      }
  
      public void setTrustStore(String trustStore) {
          this.trustStore = trustStore;
      }
  
      public void setTrustStorePass(String trustStorePass) {
          this.trustStorePass = trustStorePass;
      }
  
      public void setPackageBase(String packageBase) {
          this.packageBase = packageBase;
      }
  
      @Bean
      public Jaxb2Marshaller swpMarshaller() {
          Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
          marshaller.setPackagesToScan(packageBase);
          return marshaller;
      }
  
      @Bean
      public Unmarshaller swpUnmarshaller() throws JAXBException {
          JAXBContext jaxbContext = JAXBContext.newInstance(packageBase);
          return jaxbContext.createUnmarshaller();
      }
  
      @Bean
      public Filter encodingFilter() {
          CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
          encodingFilter.setEncoding("UTF-8");
          return encodingFilter;
      }
  
      @Bean
      public ServerProperties serverProperties() {
          ServerProperties props = new ServerProperties();
          props.setPort(port);
          props.setSsl(ssl());
  
          return props;
      }
  
      private Ssl ssl() {
          Ssl ssl = new Ssl();
          ssl.setEnabled(true);
          ssl.setKeyStore(keyStore);
          ssl.setKeyStorePassword(keyStorePass);
          ssl.setKeyStoreType("JKS");
          ssl.setKeyPassword(keyPass);
          ssl.setTrustStore(trustStore);
          ssl.setTrustStorePassword(trustStorePass);
          ssl.setClientAuth(Ssl.ClientAuth.NEED);
  
  
          return ssl;
      }
  }
  </code></pre>
  
  <p>So far, this works. But when I try to autowire a bean from the parent context, I get an error stating that there is no candidate.</p>
  
  <p>Another interesting thing is, when I inject the (child)context into one of my child context's beans using the ApplicationContextAware interface, the getParent() property of that context is null at that time.</p>
  
  <p>What I have done now is implementing getter functions like these:</p>
  
  <pre><code>private SomeBean getSomeBean() {
      if (this.someBean == null) {
          this.someBean = applicationContext.getParent().getBean(SomeBean.class);
      }
      return this.someBean;
  }
  </code></pre>
  
  <p>To summarize this: During construction of the child context's beans, the parent context is not set, so I cannot use autowire.</p>
  
  <p>Is there some way to make autowire work with my setup?</p>