谁能提供一个加载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创建的应用程序上下文中?
发布于 2016-05-13 01:11:04
如果你想使用你的类路径中的文件,你可以这样做:
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}请注意@ImportResource注释中的classpath字符串。
发布于 2015-03-22 03:36:32
您可以使用@ImportResource将配置文件导入到Spring Boot应用程序中。例如:
@SpringBootApplication
@ImportResource("applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}发布于 2019-02-23 17:53:00
注释不必是(在类上)那个(具有main方法)(具有以下调用):
SpringApplication.run(Application.class,args);
(在您的情况下,我要说的是@ImportResource不必在您的类中)
公共类ExampleApplication {}
.
你可以有一个不同的类
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}或者为了清楚起见
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class MyWhateverClassToProveTheImportResourceAnnotationCanBeElsewhere {
}这篇文章中提到了上述内容。
http://www.springboottutorial.com/spring-boot-java-xml-context-configuration
.
奖励:
如果你可能认为"SpringApplication.run“是一个无效的method.....that,事实并非如此。
您还可以执行以下操作:
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。这取决于你和谁说话,这是一件好事(别人为我做了所有好的事情),也可能是一件坏事(哇,这是许多我无法控制的依赖)。
标签:有时候看起来比窗帘更好
https://stackoverflow.com/questions/29173614
复制相似问题