0
votes

Migrating from Spring XML configuration style to Spring Java-based configuration (using @Configuration) I run into an issue loading resources, in my case from the classpath.

In XML I did have a bean declared like:

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="schema" value="classpath:/xsd/schema.xsd" />
  <property name="contextPath" value="com.company.app.jaxb" />
</bean>

Configuration this bean in a Java class looks like:

@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;
    }

This will actually throw a NullpointerException during loading of the ApplicationContext because the @Autowired field is not (yet?) autowired...

Q: What is the right solution to load resources (from the classpath and/or in general)? Using the ApplicationContext is promoted in the Spring Documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#context-introduction

Q: And why is the autowired field still null?

2
Usually Spring allows the creation of Java-based configuration via ApplicationContext context = new AnnotationConfigApplicationContext(springConfig) where springConfig is the reference to the @Configuration annotated configuration class. But I have never used it inside the configuration file itself, TBH. You could also try to initialize the marshaller in a @PostConstruct annotated method inside the configuration - Roman Vottner
I'm starting the ApplicationContext from a unit test using the @RunWith(SpringJUnit4ClassRunner.class) with @ContextConfiguration(classes = { AppConfig.class }) ... Is this applying some other lifecycle? - Marc vA

2 Answers

0
votes

For

marshaller.setSchema(applicationContext.getResource("classpath:/xsd/schema.xsd"));

you can instead use

marshaller.setSchema(new ClassPathResource("/xsd/schema.xsd"));

But I am unable to reproduce your injected ApplicationContext field being null.

0
votes

So autowiring the ApplicationContext into an @Configuration AppConfig class does work and is actually being autowired. Using it directly in an @Bean method seems to produce a circular autowiring situation. And a StackOverflowError :-(

The solution is to apply a 'post construct pattern' with @PostConstruct ...

Solution in code:

@Configuration
public class AppConfig {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.company.app.jaxb");
        return marshaller;
    }

    @PostConstruct
    public void initMarshaller() {
        marshaller().setSchema(applicationContext.getResource("classpath:/xsd/schema.xsd"));
    }