gpt4 book ai didi

java - Spring mvc Autowiring RequestMappingHandlerMapping

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:43:05 27 4
gpt4 key购买 nike

我正在尝试在我的 spring mvc Controller 中 Autowiring org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 以获得所有 url 映射并将它们显示在 UI 上,但是没有成功。存在缺少 bean 的错误:

 org.springframework.beans.factory.BeanCreationException: Could    not autowire field: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping web.controller.WorkController.handlerMapping; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我的 web.xml:

<display-name>Spring MVC Application</display-name>

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

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


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

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

我的 mvc-dispatcher-servlet.xml:

 <context:annotation-config/>
<context:component-scan base-package="web.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

我的 root-context.xml:

<bean id="helloBean" class="web.beans.HelloBean"/>

Java Controller :

package web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import web.beans.HelloBean;

import java.util.List;

@Controller
public class WorkController {

@Autowired RequestMappingHandlerMapping handlerMapping;
@Autowired private HelloBean helloBean;
@Autowired private ApplicationContext applicationContext;

@RequestMapping(value = "/index")
public String index() {
return "index";
}
}

最佳答案

您应该在 Autowiring 之前启动 RequestMappingHandlerMapping bean。它有两种方式:

  1. 在springxml配置中,比如hello bean
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<!-- add your properties here property name="..." value="..."></property-->
</bean>
  1. 或者使用

    @配置

    @Configuration 
    @ComponentScan("your.package")
    @EnableWebMvc
    public class AppConfig {
    ...
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
    // add properties here
    return mapping;
    }
    ...
    }

关于java - Spring mvc Autowiring RequestMappingHandlerMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com