无法将 Bean @Autowire到控制器中 - NoSuchBeanDefinitionException



我有一个spring-mvc web应用。

下面的代码已经被简化和净化,以保护罪犯。基本的想法是,我有两个独立的上下文配置文件:一个用于MVC,另一个用于一般配置的东西。

当Spring连接控制器时,我得到:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.ConfigBean com.example.ConfigController.config; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.example.ConfigBean] 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)}

但是,来自另一个servlet的以下代码可以正常工作:

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());            
ConfigBean config = context.getBean(ConfigBean.class);        

这对我来说意味着MVC组件扫描器由于某种原因无法看到配置内容。我试过添加

<import resource="config.xml" />

dispatcher-servlet.xml,但没有影响。反正感觉不对,因为我不想要配置bean的两个实例。即使手动将ConfigBean声明复制到dispatcher.xml中也不能解决我的问题,这表明我正在做一些非常愚蠢的事情。有什么我可能会错过的建议吗?

我的详细配置如下:

web . xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/dispatcher-servlet.xml
            /WEB-INF/config.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
... Some other non-spring stuff ...
</web-app>

dispatcher-servlet.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.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">
    <context:annotation-config />
    <context:component-scan base-package="com.example" />
    <mvc:annotation-driven />
</beans>

类路径:config . 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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd">
    <bean class="com.example.ConfigBean">
        <property name="foo" value="bar" />
    </bean>
</beans>

ConfigController.java

package com.example;
import com.example.ConfigBean
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/config")
public class ConfigController {
    @Autowired 
    private ConfigBean config;
    @RequestMapping(method=RequestMethod.GET) 
    public @ResponseBody ConfigBean getConfig() {
        return config;
    }
}

这可能是因为您在context-param中添加了/WEB-INF/dispatcher-servlet.xmlcontextConfigLocation

这不是必需的,因为web应用程序上下文将根据显示程序servlet名称

准备文件。

选自Spring Doc

在DispatcherServlet初始化时,Spring MVC查找一个.xml目录下名为[servlet-name]-servlet.xml的文件Web应用程序并创建在那里定义的bean,覆盖对象中使用相同名称定义的任何bean的定义范围。

我认为由于存在多个ConfigBean实例,自动装配失败。错误信息有点令人困惑。如果bean丢失或有多个实例,则会得到相同的错误。

问题在于dispatcher-servlet.xml中的组件扫描。假设ConfigBean类也用@Component或它的一个子类型注释,将创建两个实例。一次用于config.xml中的bean定义,一次用于组件扫描。

要解决这个问题,你需要修改组件扫描来忽略非控制器类。

<context:component-scan  user-default-filters='false'>
  <context:include-filter annotation="org.springframework.stereotype.Controller"/>
</context:component-scan>

另一个servlet类中的查找正在工作,因为您正在直接访问根应用程序上下文。

这确实是相当愚蠢的。<context:annotation-config />指令应该在config.xml中,而不是dispatcher-servlet.xml中。

据我所知,注释的东西是在父上下文中,所以那是指令所属的地方。

最新更新