我需要将应用程序从 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 没有提供等价的 API。
所以没有直接的方法来提供 XML Spring 配置文件。
根据这个answer ,您可以使用 @ImportResource
注释您的 Spring Boot 类.
@ImportResource("classpath:/META-INF/application-context-nodb.xml")
它的工作方式与 @Import
相同,但它导入 XML spring 配置文件而不是类文件。
Javadoc 信息:
Indicates one or more resources containing bean definitions to import.
Like
@Import
, this annotation provides functionality similar to the element in Spring XML. It is typically used when designing@Configuration
classes to be bootstrapped by anAnnotationConfigApplicationContext
, but where some XML functionality such as namespaces is still necessary.
关于java - spring-boot-2.0.0.RELEASE 中缺少 SpringApplication.run 的特定重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49379657/