使用 SpringApplication 时加载 applicationcontext.xml [英] Loading applicationcontext.xml when using SpringApplication

查看:46
本文介绍了使用 SpringApplication 时加载 applicationcontext.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能提供一个加载 applicationContext.xml 文件的 SpringApplication 示例?

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

我正在尝试使用 Spring 的示例(基于 Gradle).我有一个 applicationContext.xml,但我不知道如何让 SpringApplication 加载它.通过

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

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

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

推荐答案

如果您想使用类路径中的文件,您可以随时这样做:

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

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

Notice the classpath string in @ImportResource annotation.

这篇关于使用 SpringApplication 时加载 applicationcontext.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆