Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading applicationcontext.xml when using SpringApplication

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?

like image 995
Raster Avatar asked Mar 20 '15 18:03

Raster


People also ask

Where do I put ApplicationContext xml?

It needs to be in the classpath.

Is ApplicationContext xml required?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app.

What is required to load the beans configured in ApplicationContext xml file?

ClassPathXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look like bean configuration XML file in CLASSPATH.

What happens when we call SpringApplication run () method?

SpringApplication. run(Classname. class, args) bootstraps a spring application as a stand-alone application from the main method. It creates an appropriate ApplicationContext instance and load beans.


1 Answers

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.

like image 85
Tomasz Dzieniak Avatar answered Oct 19 '22 12:10

Tomasz Dzieniak