1、缘由。
今天尝试springboot4.0.0-snapshot 版本时,尝试他的版本控制功能。
上午启动还好好的,下午就报错:
2、报错信息:
java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:353) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.core.annotation.TypeMappedAnnotation.adapt(TypeMappedAnnotation.java:451) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.core.annotation.TypeMappedAnnotation.getValue(TypeMappedAnnotation.java:384) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.core.annotation.TypeMappedAnnotation.asMap(TypeMappedAnnotation.java:273) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.core.annotation.AbstractMergedAnnotation.asAnnotationAttributes(AbstractMergedAnnotation.java:191) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.AnnotationBeanNameGenerator.determineBeanNameFromAnnotation(AnnotationBeanNameGenerator.java:143) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.AnnotationBeanNameGenerator.generateBeanName(AnnotationBeanNameGenerator.java:110) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.registerBeanDefinitionForImportedConfigurationClass(ConfigurationClassBeanDefinitionReader.java:167) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:147) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:126) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:446) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:306) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:349) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:118) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:781) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:599) ~[spring-context-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.boot.web.server.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-web-server-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) ~[spring-boot-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:435) ~[spring-boot-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1344) ~[spring-boot-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1333) ~[spring-boot-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at com.nextera.boot4.Boot4Application.main(Boot4Application.java:15) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ~[na:na]
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:534) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:513) ~[na:na]
at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:121) ~[spring-boot-devtools-4.0.0-SNAPSHOT.jar:4.0.0-SNAPSHOT]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ~[na:na]
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:534) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:513) ~[na:na]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:302) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:343) ~[spring-core-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT]
... 25 common frames omitted
Process finished with exit code 0
3、分析:
查了一下:
真正的WebMVC配置由 org.springframework.boot.webmvc.autoconfigure.WebMvcAutoConfiguration 提供。
4、 解决方法:
手动创建假的包结构和文件。
在 src/main/java/org/springframework/boot/autoconfigure/web/servlet 目录下,创建
文件:
WebMvcAutoConfiguration.java
内容如下:
@Configuration
@ConditionalOnMissingBean(name = "webMvcAutoConfiguration")
public class WebMvcAutoConfiguration {
// 这是一个空的占位符类,只用于解决硬编码类引用问题
// 真正的WebMVC配置由 org.springframework.boot.webmvc.autoconfigure.WebMvcAutoConfiguration 提供
}
启动又报错:
java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration]
5. 如法炮制:
在src/main/java/org/springframework/boot/autoconfigure/orm/jpa 目录下,创建
HibernateJpaAutoConfiguration.java 文件, 内容如下:
@Configuration
@ConditionalOnMissingBean(name = "hibernateJpaAutoConfiguration")
public class HibernateJpaAutoConfiguration {
// 这是一个空的占位符类,只用于解决硬编码类引用问题
// 真正的JPA配置由Spring Boot 4.0的新模块化架构提供
}
6、至此,启动成功。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v4.0.0-SNAPSHOT)
2025-07-02T17:32:28.757+08:00 INFO 21936 --- [boot4] [ restartedMain] [ ] com.nextera.boot4.Boot4Application : Starting Boot4Application using Java 21.0.5 with PID 21936 (D:\practice\springDeepSearch\boot4\target\classes started by wen in D:\practice\springDeepSearch\boot4)
2025-07-02T17:32:28.766+08:00 INFO 21936 --- [boot4] [ restartedMain] [ ] com.nextera.boot4.Boot4Application : The following 1 profile is active: "local"
2025-07-02T17:32:33.121+08:00 WARN 21936 --- [boot4] [ restartedMain] [ ] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with an AuthenticationProvider bean. UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. Consider removing the AuthenticationProvider bean. Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider. If the current configuration is intentional, to turn off this warning, increase the logging level of 'org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer' to ERROR
2025-07-02T17:32:35.102+08:00 INFO 21936 --- [boot4] [ restartedMain] [ ] com.nextera.boot4.Boot4Application : Started Boot4Application in 6.968 seconds (process running for 8.56)
有时候不要轻易添加新的代码,尝试新功能,现在终于理解了为什么windows 系统有安全配置,最后一次正确配置了。如果Idea 有这个功能就好了。