Spring Boot 1.2.5升级至2.0.0时SpringApplication.run重载缺失问题
Hey there! Let's figure out why that SpringApplication.run overload is missing in Spring Boot 2.0.0.RELEASE and how to fix it.
Spring Boot 2.0 made intentional changes to simplify the SpringApplication class, which included removing a few less commonly used overloads of the run method that existed in 1.x versions like 1.2.5.RELEASE. Let's break down the most likely scenarios and their fixes based on your code:
Common Scenario 1: You were using an overload that accepted arbitrary objects (like XML config paths directly)
Looking at your code, you have an APPLICATION_CONTEXT_XML constant pointing to a Spring XML config file. If your original run call looked something like this (passing the XML path directly):
SpringApplication.run(APPLICATION_CONTEXT_XML, args);
Or maybe passing an instance of your configuration class instead of its Class object:
SpringApplication.run(new NiceBootApplicationWithoutDB(), args);
These overloads were removed in Spring Boot 2.0. Here's how to adjust:
Fix: Use @ImportResource for XML config
The cleanest way to load your XML context in Spring Boot 2.0 is to add the @ImportResource annotation directly to your configuration class. Update your NiceBootApplicationWithoutDB like this:
@Configuration @ComponentScan @EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class}) @ImportResource(APPLICATION_CONTEXT_XML) // Add this line to load your XML config 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) { // Use the standard, still-existing overload SpringApplication.run(NiceBootApplicationWithoutDB.class, args); } }
Common Scenario 2: You were using the args-only run overload
If your original code called SpringApplication.run(args) without specifying the primary configuration class, this overload was removed in Spring Boot 2.0. The framework now requires you to explicitly provide the main configuration class to bootstrap from.
Fix: Pass your main class to run
Simply update the call to include your NiceBootApplicationWithoutDB class:
SpringApplication.run(NiceBootApplicationWithoutDB.class, args);
Bonus: Alternative with SpringApplicationBuilder
If you prefer a programmatic approach instead of annotations (for example, if you need conditional logic to load the XML config), you can use SpringApplicationBuilder to configure and run your app:
public static void main(String[] args) { new SpringApplicationBuilder(NiceBootApplicationWithoutDB.class) .sources(new ClassPathResource(APPLICATION_CONTEXT_XML)) .run(args); }
Just double-check that your AbstractBootApplication class is compatible with Spring Boot 2.0 too—if it extends SpringBootServletInitializer, there are minor signature changes to the configure method you might need to adjust, but that's a separate issue from the run overload.
内容的提问来源于stack exchange,提问作者Stav Alfi

