我有一个 spring-mvc 网络应用程序。
以下代码已被简化和清理以保护有罪者。基本思想是我有两个独立的上下文配置文件:一个用于 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>
调度程序-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>
配置控制器.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;
}
}