Spring Boot 2.0.1中SpringApplicationBuilder类及configure方法不存在问题咨询
SpringApplicationBuilder & configure Method in Spring Boot 2.0.1 Hey there, let's break down why you're hitting this issue with your ServletInitializer class. The most common culprits here are incorrect dependencies, wrong package imports, or subtle version conflicts—let's go through each fix step by step.
1. Verify Your Dependencies Are Correct
First, make sure your build file (Maven pom.xml or Gradle build.gradle) includes the necessary Spring Boot core and web dependencies.
For Maven:
Add these entries to your pom.xml (adjust scope if you're building a WAR):
<!-- Core Spring Boot web starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Required for WAR packaging (mark as provided to avoid conflicting with server's Tomcat) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
For Gradle:
Add these lines to your build.gradle:
implementation 'org.springframework.boot:spring-boot-starter-web' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // For WAR builds
Run a clean build afterward (mvn clean install for Maven, ./gradlew clean build for Gradle) to ensure dependencies are properly resolved.
2. Check Your Package Imports
In Spring Boot 2.x, the SpringBootServletInitializer moved to a new package—this is a super common mistake! Make sure your ServletInitializer class uses the correct imports:
// Correct imports for Spring Boot 2.x import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootMainApplication.class); } }
Note: In Spring Boot 1.x, SpringBootServletInitializer was in org.springframework.boot.context.web—that package no longer exists in 2.x, so using it will cause missing method/class errors.
3. Confirm Your Main Application Class Is Properly Annotated
Double-check that your SpringBootMainApplication class has the @SpringBootApplication annotation, which is required for Spring Boot to bootstrap correctly:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootMainApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMainApplication.class, args); } }
4. Rule Out Dependency Conflicts
If the above steps don't work, check for conflicting dependencies in your project. Old Spring Boot 1.x artifacts or conflicting Spring Framework versions can cause classloading issues. Use your build tool's dependency tree command to spot conflicts:
- Maven:
mvn dependency:tree - Gradle:
./gradlew dependencies
Look for any entries with Spring Boot versions older than 2.0.1 and exclude them if necessary.
内容的提问来源于stack exchange,提问作者petmik

