【问题标题】:@RequestMapping annotation not working if <context:component-scan /> is in application context instead of dispatcher context如果 <context:component-scan /> 在应用程序上下文而不是调度程序上下文中,@RequestMapping 注释不起作用
【发布时间】:2014-05-18 14:17:25
【问题描述】:

我正在使用带有 和 @Autowired 的 Spring 2.5.6 版。

虽然我一直在调度程序上下文中使用 SimpleUrlHandlerMapping,但一切正常 - 自动装配工作正常,路线以正确的方式选择控制器等。

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>
            <entry key="/login/login.html" value-ref="loginController" />
            <entry key="/secured/index/index.html" value-ref="indexController" />
        </map>
    </property>
</bean>

然后我决定使用 @RequestMapping 在 XML 文件中配置我的路由。

@Controller("loginController")
public class LoginController {

    @RequestMapping("/login/login.html")
    public ModelAndView login() {
        ModelAndView model = new ModelAndView("login/login");
        return model;
    }

}

当我重写代码以使用 @RequestMapping 时,问题开始了。服务器(Tomcat)开始给我一个“No mapping found for HTTP request with URI”错误。

我发现解决方案是在我的调度程序上下文配置中插入一个 "" 元素.

<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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- ============== -->
    <!-- URL Mapping    -->
    <!-- ============== -->

<!--    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> -->
<!--        <property name="urlMap"> -->
<!--            <map> -->
<!--                <entry key="/login/login.html" value-ref="loginController" /> -->
<!--                <entry key="/secured/index/index.html" value-ref="indexController" /> -->
<!--            </map> -->
<!--        </property> -->
<!--    </bean> -->

    <!-- ============ -->
    <!-- viewResolver -->
    <!-- ============ -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <context:component-scan base-package="com.example.springwebapp.controller" />

</beans>

我觉得这可能是一个糟糕的解决方案,因为我的应用上下文配置文件中有类似的 ""一次性处理所有其他依赖项。

<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <import resource="./spring-security.xml"/>
    <import resource="../database/DataSource.xml"/>
    <import resource="../database/Hibernate.xml"/>

    <context:component-scan base-package="com.example.springwebapp" />

</beans>

问题是为什么 在应用程序级别没有选择 @RequestMapping 注释对吗?我知道我可以将 base-package 在应用级别的组件扫描限制为某个包,但我的意图是在应用级别仅使用一个 component-scan 仅此而已.我真的必须使用两个不同的 component-scan 元素还是我遗漏了什么?

这是我的 web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>SpringWebApp</display-name>

    <!-- ========== -->
    <!-- Spring MVC -->
    <!-- ========== -->

    <welcome-file-list>
        <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Application</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring/mvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Application</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/spring/application-context.xml
        </param-value>
    </context-param>

    <!-- =============== -->
    <!-- Spring Security -->
    <!-- =============== -->

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

【问题讨论】:

  • 你能发布你的 Web.xml 吗?
  • 当然 - 在我的问题末尾插入它。

标签: java spring spring-mvc


【解决方案1】:

尝试将此标签添加到您的应用上下文中:

<mvc:annotation-driven />

编辑

对于Spring 2.5,尝试将注释配置标签、DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter bean 添加到您的应用上下文中:

<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

【讨论】:

  • 我在文档中发现 可从 Spring 3 开始使用。我尝试添加 (在 2.5 中可用),但这无济于事。
  • 抱歉没有看到您使用的是 Spring 2.5
  • 添加 (不需要其他两个标签)有帮助。谢谢!但我不明白一件事。在这里docs.spring.io/spring/docs/2.5.6/api/org/springframework/web/… 他们写道,如果我定义了一个自定义 HandlerMapping,我只需要包含该标签,否则它会被隐式包含。这不是我的情况,为什么我需要明确包含它?
  • 另一个奇怪的事情是我在网络上的某个地方发现 DefaultAnnotationHandlerMa‌​pping 应该在调度程序上下文中定义,但在我的情况下,它只有在应用程序上下文中才有效(如您建议的那样) .
  • 你是对的,DefaultAnnotationHandlerMa‌​pping 将被隐式包含。但是在 Spring 2.5 中,它只有在 mvc-context 中有 context:component-scan 标签时才有效。由于您只想将 context:component-scan 标签放在应用程序上下文中,因此您必须显式声明它。这是一种解决方法,可以使其在两个上下文中都没有 context:component-scan 标签的情况下工作。
【解决方案2】:

在阅读了我的问题的答案(非常感谢 Jean-Philippe Bond)后,我决定改变我的方法。我知道有两个单独的 context:component-scan 是正确的方法。

现在我的应用上下文得到了:

<context:component-scan base-package="com.example.springwebapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
        <context:include-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
</context:component-scan>

而我的 调度程序上下文 得到了:

<context:component-scan base-package="com.example.springwebapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

【讨论】:

    猜你喜欢
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    相关资源
    最近更新 更多