首页 > 解决方案 > Spring MVC 组件扫描不拾取控制器

问题描述

全部,

我正在尝试设置一个新的 Spring MVC 项目并创建了一个虚拟控制器。我面临的问题是 Spring 没有创建控制器的 bean。否则构造函数中的日志语句应该已经打印出来了。控制器的包也定义在 bean 配置 xml 中。

<context:component-scan base-package="com.blah.apps" annotation-config="true"/>

Spring 版本:5.2.8 Tomcat 版本:8

我使用上述版本并使用基于 xml 的配置有一个特定的原因。

我得到的错误是:

Aug 25, 2020 10:53:02 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /[app-name]/health

考虑到 bean 没有被创建,这并不奇怪。

请在下面找到代码...

package com.blah.apps.xyz.shopping.health;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HealthController {

public HealthController() {
        System.out.println("Event this is not getting printed...the component scan should allow spring bean discovery..");
    }


    @ResponseBody
    @RequestMapping(value = "/health")
    public String getHealth() {
        return "App is running!";
    }
}

我的 Web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <absolute-ordering />
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

dispatcherServlet-servlet.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />

<!-- have tried removing annotation-config as well-->

    <context:component-scan
        base-package="com.blah.apps" annotation-config="true"/> 
</beans>

标签: javaxmlspringspring-mvc

解决方案


您需要在 web.xml 中添加一个侦听器来加载 Spring 上下文。

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>
</context-param>

<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
  </init-param>
</servlet>

推荐阅读