使用SpringApplication时加载applicationcontext.xml

Ras*_*ter 8 applicationcontext spring-boot

任何人都可以提供一个加载applicationContext.xml文件的SpringApplication示例吗?

我正在尝试使用Spring的示例(基于Gradle)将我的GWT RPC应用程序移动到RESTful Web服务.我有一个applicationContext.xml,但我没有看到如何让SpringApplication加载它.通过手动加载

ApplicationContext context = new ClassPathXmlApplicationContext(args);

导致空的背景.......即使它起作用,也会与返回的那个分开

SpringApplication.run(Application.class, args);

或者有没有办法让外部bean进入SpringApplication.run创建的应用程序上下文?

Tom*_*iak 12

如果您想使用类路径中的文件,您可以随时执行此操作:

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

注意注释中的classpath字符串@ImportResource.


And*_*son 8

您可以使用@ImportResource将XML配置文件导入Spring Boot应用程序.例如:

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

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

}
Run Code Online (Sandbox Code Playgroud)