Spring Boot启动报错:缺少mvcHandlerMappingIntrospector Bean及自动关闭问题
Spring Boot 3.0.1 启动问题排查与解决
问题1:未配置mvcHandlerMappingIntrospector Bean启动报错
错误现象
启动时抛出如下异常:
Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector required to use MvcRequestMatcher
原因
Spring Boot 3.x整合Spring Security时,若使用MvcRequestMatcher(比如Security配置里的requestMatchers(...)规则),容器需要HandlerMappingIntrospector Bean解析请求映射,但该版本默认不再自动注册这个Bean。
解决方法
在Security配置类中手动注册该Bean:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.HandlerMappingIntrospector; @Configuration public class SecurityConfiguration { @Bean(name = "mvcHandlerMappingIntrospector") public HandlerMappingIntrospector mvcHandlerMappingIntrospector() { return new HandlerMappingIntrospector(); } // 你的其他Security配置示例 /* @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(auth -> auth .requestMatchers("/public/**").permitAll() .anyRequest().authenticated()) .formLogin(form -> form.permitAll()); return http.build(); } */ }
问题2:配置Bean后应用启动成功随即自动关闭
现象
启动日志显示应用启动成功,但进程立刻终止,没有持续运行。
原因
核心是Web容器(如Tomcat)未启动,常见诱因:
- 缺失
spring-boot-starter-web依赖,导致Web环境未激活 - 配置文件中手动禁用了Web环境
- 启动类注解配置错误
解决方法
- 检查Web依赖:确保pom.xml中引入
spring-boot-starter-web:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
- 检查Web环境配置:查看
application.properties/application.yml,若存在如下配置,删除或修改:
# 错误配置,会禁用Web环境 spring.main.web-application-type=none # 修改为(或直接删除,默认就是servlet) spring.main.web-application-type=servlet
- 检查启动类:确保启动类标注
@SpringBootApplication注解,无错误配置:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
内容的提问来源于stack exchange,提问作者Guru Kiran
相关产品推荐
相关产品推荐

