You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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环境
  • 启动类注解配置错误

解决方法

  1. 检查Web依赖:确保pom.xml中引入spring-boot-starter-web:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 检查Web环境配置:查看application.properties/application.yml,若存在如下配置,删除或修改:
# 错误配置,会禁用Web环境
spring.main.web-application-type=none

# 修改为(或直接删除,默认就是servlet)
spring.main.web-application-type=servlet
  1. 检查启动类:确保启动类标注@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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:50:16