【问题标题】:specific overload of SpringApplication.run is missing in spring-boot-2.0.0.RELEASEspring-boot-2.0.0.RELEASE 中缺少 SpringApplication.run 的特定重载
【发布时间】:2018-08-28 23:57:39
【问题描述】:

我需要将应用程序从 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 中不存在这两个重载。

我的问题是——如何升级以上代码?

【问题讨论】:

  • SpringApplication constructorrun 方法现在使用 Class<?>[] primarySources 参数...你试过了吗?

标签: java spring spring-boot


【解决方案1】:

您是对的:Spring Boot 版本 2 中 SpringApplication 类的 API 不提供等效项。
所以没有直接的方式来提供一个 XML Spring 配置文件。

根据这个answer,您可以使用@ImportResource 注释您的Spring Boot 类。

@ImportResource("classpath:/META-INF/application-context-nodb.xml")

它作为@Import 工作,但它导入 XML spring 配置文件而不是类文件。

Javadoc 信息

指示一个或多个包含要导入的 bean 定义的资源。

@Import 一样,此注解提供的功能类似于 Spring XML 中的元素。它通常在设计时使用 @Configuration 类被引导 AnnotationConfigApplicationContext,但是有一些 XML 功能 例如命名空间仍然是必要的。

【讨论】:

    【解决方案2】:

    可以使用注解@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));
            }        
        }
    

    【讨论】:

    • 为什么你使用SpringApplication.main而不是SpringApplication.run
    • 对不起,从我的IDE复制过去...当然我们应该使用运行,没有配置源类它不会工作。如果我们想使用 main 我们应该在 main 方法的参数中添加配置
    【解决方案3】:

    您可以像这样添加一个配置类,它将考虑您的 spring xml 配置:)

    @Configuration
    @ImportResource({"classpath*:applicationContext.xml"})
    public class XmlConfiguration {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-28
      • 2018-12-31
      • 1970-01-01
      • 2018-05-19
      • 2016-06-15
      • 1970-01-01
      • 2018-05-21
      相关资源
      最近更新 更多