gpt4 book ai didi

java - 无法将 bean @Autowire 到 Controller 中 - NoSuchBeanDefinitionException

转载 作者:行者123 更新时间:2023-12-01 23:45:25 26 4
gpt4 key购买 nike

我有一个 spring-mvc Web 应用程序。

以下代码已被简化和净化以保护犯罪者。基本思想是我有两个单独的上下文配置文件:一个用于 MVC,另一个用于一般配置内容。

当 Spring 连接 Controller 时,我得到:

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>

调度程序-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;
}
}

最佳答案

可能是因为您已将 /WEB-INF/dispatcher-servlet.xml 添加到 context-param 中的 contextConfigLocation

这不是必需的,因为 Web 应用程序上下文将根据 displater servlet 名称准备好文件

来自Spring Doc

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

关于java - 无法将 bean @Autowire 到 Controller 中 - NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140085/

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