java - 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>
解决方案
您需要在 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>
推荐阅读
- matlab - 使用 ode45 求解状态空间方程时出错
- python - 无法重建分解图像
- node.js - Json 对象数组:用它们的值替换“键”名称以避免相同的键名和易于检索
- python - 当详细信息来自数据库时,电子邮件发送 django 返回错误
- reactjs - 在 webpack 中使用 TypeScript、styled-components 和 PostCSS
- c++builder - 如何将 kylix3 gui 项目 (c++) 移植到 linux 中任何其他 c++ 支持的 gui ide?
- instagram - 如何为 Instagram Basic Display API App Review 指定测试帐户
- laravel - Laravel 是否支持嵌套 GroupBy?
- javascript - 如何在html a href标签中添加多个javascript变量
- python - python方法中的(self)和(self,)有什么区别?