title: Spring Java-based configuration loading resources (from classpath)
body: <p>Migrating from Spring XML configuration style to Spring Java-based configuration (using <code>@Configuration</code>) I run into an issue loading resources, in my case from the classpath.</p>
  
  <p>In XML I did have a bean declared like:</p>
  
  <pre><code>&lt;bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&gt;
    &lt;property name="schema" value="classpath:/xsd/schema.xsd" /&gt;
    &lt;property name="contextPath" value="com.company.app.jaxb" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Configuration this bean in a Java class looks like:</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Autowired
      private ApplicationContext applicationContext;
  
      @Bean
      public Marshaller marshaller() {
          Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
          marshaller.setSchema(applicationContext.getResource("classpath:/xsd/schema.xsd"));
          marshaller.setContextPath("com.company.app.jaxb");
          return marshaller;
      }
  </code></pre>
  
  <p>This will actually throw a NullpointerException during loading of the <code>ApplicationContext</code> because the <code>@Autowired</code> field is not (yet?) autowired...</p>
  
  <p>Q: What is the right solution to load resources (from the classpath and/or in general)? Using the <code>ApplicationContext</code> is promoted in the Spring Documentation: <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#context-introduction" rel="nofollow">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#context-introduction</a></p>
  
  <p>Q: And why is the autowired field still null?</p>