我的应用程序正确运行:
<context:component-scan base-package="com.mypackage"> </context:component-scan>
当我用手动 bean 定义替换它时,不再检测到 Controller 。
在任何情况下我都会使用这些注释:
<context:annotation-config />
<mvc:annotation-driven />
调用 Controller 的 Autowiring 方法,但 Bean 未声明为入口点,因此会出现 404 错误并且无法访问。
组件扫描背后的黑魔法是什么?
Controller 的声明如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Controller configuration -->
<bean class="com.xx.ControllerClass" />
</beans>
最佳答案
根据您的评论推测 - 您已在 applicationContext-controllers.xml 文件中声明了 Controller 的 beans,现在该文件已导入到 Web 应用程序上下文文件中,即您使用 DispatcherServlet web.xml 文件声明的文件:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext-controller.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
如果不是,那么这可能就是问题所在。基于 Spring MVC 的应用程序通常有两种不同的应用程序上下文,一种是使用 ContextLoaderListener(根 Web 应用程序上下文)声明的,另一种是通过 DispatcherServlet 声明的 Web 相关 bean、 Controller 、mvc:annotation-driven
等需要在 web 相关的 bean 声明中。
关于java - Spring禁用上下文:component-scan does not detect controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418478/