title: Spring JavaConfig + WebMvcConfigurerAdapter + @Autowired => NPE
body: <p>I have an application with 2 Contexts. Parent for web agnostic business logic and ChildContext (implicitly created by dispatcher servlet) for web logic.</p>
  
  <p>My setup loks like</p>
  
  <pre><code>@Configuration
  public class BusinessConfig {
     @Bean 
     public ObjectMapper jacksonMapper() { return new ObjectMapper() } 
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Configuration
  public class WebConfig extends WebMvcConfigurerAdapter {
      @Autowired
      private ObjectMapper objectMapper; // &lt;- is null for some reason
      @Override
      public configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters)    {
           MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
           converter.setObjectMapper(objectMapper); // &lt;- bang!
           messageConverters.add(converter);
      } 
  }
  </code></pre>
  
  <p>I need the the object mapper in the parent context, as I use it also in security configuration. But can someone explain me, why the @Autowired objectMapper is null? Its created in the parent context (the fact that the parent exists is even logged by spring at startup). Also @Autowired has required=true by default, so it should not blow up in the configure method (it should have blown up in construction of the context, if the bean wasn't there for some reason).</p>
  
  <p>It seems to me that there might be some lifecycle problem in spring - in a sense that it calls the overridden methods first, and then @Autowires the dependencies... I have also tried to @Autowire the BusinessConfig (should be perfectly legal according to documentation - the result was the same (null)).</p>
  
  <p>What should I do to make this working?</p>
  
  <p>Thanks in advance!</p>
  
  <h2><strong>EDIT - ISSUE FOUND</strong></h2>
  
  <p>I found the issue. Unfortunately it had nothing to do with WebMvcConfigurerAdapter nor @Configuration. It was caused by premature initialization of context triggered by missing static modifier for propertyPlaceholderConfigurer... I have created issue in Spring core jira (<a href="https://jira.spring.io/browse/SPR-14382" rel="nofollow">https://jira.spring.io/browse/SPR-14382</a>)</p>