首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用SpringApplication时加载applicationcontext.xml

在使用SpringApplication时加载applicationcontext.xml
EN

Stack Overflow用户
提问于 2015-03-21 02:38:43
回答 4查看 25.5K关注 0票数 9

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

我正在尝试使用Spring's Example (基于RESTful )将我的GWT RPC应用程序迁移到Gradle web服务。我有一个applicationContext.xml,但我不知道如何让SpringApplication加载它。通过手动加载

ApplicationContext context = new ClassPathXmlApplicationContext(args);

结果为空的上下文。...and,即使它可以工作,它也会与从

SpringApplication.run(Application.class, args);

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

EN

回答 4

Stack Overflow用户

发布于 2016-05-13 01:11:04

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

代码语言:javascript
复制
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class ExampleApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleApplication.class, args);
    }
}

请注意@ImportResource注释中的classpath字符串。

票数 15
EN

Stack Overflow用户

发布于 2015-03-22 03:36:32

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

代码语言:javascript
复制
@SpringBootApplication
@ImportResource("applicationContext.xml")
public class ExampleApplication {

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

}
票数 9
EN

Stack Overflow用户

发布于 2019-02-23 17:53:00

注释不必是(在类上)那个(具有main方法)(具有以下调用):

SpringApplication.run(Application.class,args);

(在您的情况下,我要说的是@ImportResource不必在您的类中)

公共类ExampleApplication {}

.

你可以有一个不同的类

代码语言:javascript
复制
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}

或者为了清楚起见

代码语言:javascript
复制
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class MyWhateverClassToProveTheImportResourceAnnotationCanBeElsewhere {
}

这篇文章中提到了上述内容。

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

.

奖励:

如果你可能认为"SpringApplication.run“是一个无效的method.....that,事实并非如此。

您还可以执行以下操作:

代码语言:javascript
复制
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);
        }

这也会让你巧妙地了解spring boot带来的许多、很多、很多(我有提到“很多”吗?)....dependencies。这取决于你和谁说话,这是一件好事(别人为我做了所有好的事情),也可能是一件坏事(哇,这是许多我无法控制的依赖)。

标签:有时候看起来比窗帘更好

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29173614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档