![](/img/trans.png)
[英]Other ways of kickstarting Spring Boot instead of SpringApplication.run
[英]specific overload of SpringApplication.run is missing in spring-boot-2.0.0.RELEASE
我需要将应用程序从spring-boot-1.2.5.RELEASE升级到spring-boot-2.0.0.RELEASE。
我有以下代码:
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class})
public class NiceBootApplicationWithoutDB extends AbstractBootApplication {
public static final String APPLICATION_CONTEXT_XML = "classpath:/META-INF/application-context-nodb.xml";
public static void main(String[] args) {
SpringApplication.run(APPLICATION_CONTEXT_XML, getFullArgList(args));
}
}
SpringApplication.run(APPLICATION_CONTEXT_XML, getFullArgList(args))
的重载为:
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings.
* @param source the source to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Object source, String... args) {
return run(new Object[] { source }, args);
}
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified sources using default settings and user supplied arguments.
* @param sources the sources to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
return new SpringApplication(sources).run(args);
}
这两个重载都不在spring-boot-2.0.0.RELEASE中。
我的问题是-如何升级以上代码?
您是对的:Spring Boot版本2中的SpringApplication
类的API没有提供等效功能。
因此,没有直接的方法来提供XML Spring配置文件。
根据这个答案 ,您可以使用@ImportResource
注释您的Spring Boot类。
@ImportResource("classpath:/META-INF/application-context-nodb.xml")
它作为@Import
起作用,但是它导入XML spring配置文件而不是类文件。
Javadoc信息 :
指示一个或多个包含要导入的bean定义的资源。
与
@Import
一样,此批注提供的功能类似于Spring XML中的元素。 通常,在设计要由AnnotationConfigApplicationContext
引导的@Configuration
类时,通常使用它,但是在某些地方仍需要诸如名称空间之类的XML功能。
您可以使用注释@ImportResource
导入配置XML
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class})
@ImportResource(APPLICATION_CONTEXT_XML)
public class NiceBootApplicationWithoutDB extends AbstractBootApplication {
public static final String APPLICATION_CONTEXT_XML = "classpath:/META-INF/application-context-nodb.xml";
public static void main(String[] args) {
SpringApplication.run(AbstractBootApplication.class, getFullArgList(args));
}
}
您可以添加一个这样的Configuration类,这将考虑到您的spring xml配置:)
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.