Loading applicationcontext.xml when using SpringApplication

25,776

Solution 1

If you'd like to use file from your classpath, you can always do this:

@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class ExampleApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleApplication.class, args);
    }
}

Notice the classpath string in @ImportResource annotation.

Solution 2

You can use @ImportResource to import an XML configuration file into your Spring Boot application. For example:

@SpringBootApplication
@ImportResource("applicationContext.xml")
public class ExampleApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleApplication.class, args);
    }

}
Share:
25,776
Raster
Author by

Raster

A lifetime Linux user since 1992. Currently working as a software engineer for BT.

Updated on July 22, 2022

Comments

  • Raster
    Raster almost 2 years

    Could anyone provide an example of a SpringApplication that loads an applicationContext.xml file?

    I'm attempting to move my GWT RPC application to a RESTful web service by using a Spring's Example (Gradle based). I have an applicationContext.xml but I do not see how to get SpringApplication to load it. Loading manually via

    ApplicationContext context = new ClassPathXmlApplicationContext(args);

    results in an empty context. ...and even if that worked it would be separate from the one returned from

    SpringApplication.run(Application.class, args);

    Or is there a way to get external beans into the app context created by SpringApplication.run?