Loading applicationcontext.xml when using SpringApplication

Can someone provide an example of SpringApplication that loads the applicationContext.xml file?

I'm trying to port a GWT RPC application to a RESTful web service using the Spring example (Gradle), I have applicationContext.xml, but I don't see how to get SpringApplication to load it. Manual download via

ApplicationContext context = new ClassPathXmlApplicationContext(args);

leads to an empty context .... and even if it worked, it would be separate from the one that was returned from

SpringApplication.run(Application.class, args);

Or is there a way to get external beans in the context of the application created by SpringApplication.run?

+7
spring-boot applicationcontext
source share
3 answers

You can use @ImportResource to import the 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); } } 
+8
source share

If you want to use the 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 line in the @ImportResource annotation.

+8
source share

Thanks Andy, this made him very brief. However, my main problem turned out to be that the application applicationContext.xml gets into the classpath.

Apparently, placing files in src/main/resources is required to get them in the classpath (by putting them in a jar). I tried to install CLASSPATH, which was simply ignored. In my example above, the download seemed to fail. Using @ImportResource made it fail in verbal form (which helped me track the real cause).

-one
source share

All Articles