9

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?

Raster
  • 157
  • 1
  • 2
  • 7

4 Answers4

17

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.

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
9

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

}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
0

The annotation does not have to be (on the class) that (has the main method) that (has this below call):

SpringApplication.run(Application.class, args);

(in your case, what I am saying is that @ImportResource does NOT have to be on your class)

public class ExampleApplication {}

.........

You can have a different class

@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}

or for clarity

@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class MyWhateverClassToProveTheImportResourceAnnotationCanBeElsewhere {
}

The above is mentioned in this article

http://www.springboottutorial.com/spring-boot-java-xml-context-configuration

.........

BONUS:

And just in case you may have thought "SpringApplication.run" was a void method.....that is NOT the case.

You can also do this:

public static void main(String[] args) {

        org.springframework.context.ConfigurableApplicationContext applicationContext = SpringApplication.run(ExampleApplication.class, args);

        String[] beanNames = applicationContext.getBeanDefinitionNames();
        Arrays.sort(beanNames);

        for (String name : beanNames) {
            System.out.println(name);
        }

This will also subtly clue you in to all the many, many, many (did I mention "many"?)....dependencies that spring boot is bringing in. Depending to whom you speak, this is a good thing (somebody else did all the nice figuring out for me) or an evil thing (whoah, that's a lot of dependencies that I don't control).

hashtag:sometimesLookBehindTheCurtain

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
-1

Thanks Andy, that made it very concise. However, my main problem turned out to be getting applicationContext.xml into the classpath.

Apparently, putting files into src/main/resources is required to get them into the classpath (by placing them into the jar). I was attempting to set CLASSPATH which was just ignored. In my example above, the load seemed to fail silently. Using @ImportResource caused it to fail verbosely (which helped me track down the real cause).

Raster
  • 157
  • 1
  • 2
  • 7