【发布时间】:2015-06-26 08:28:18
【问题描述】:
我有一个 Spring MVC 应用程序,我试图在使用 @Controller 注释的类中使用 @Autowired。在没有@Controller 注释的类中,@Autowired 工作正常,只要我添加了@Controller 注释,我就会在启动时得到一个巨大的堆栈跟踪,主要归结为No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] found for dependency
我感觉到 Spring 正在尝试自动连接我的依赖两次? 我将在我的问题中进一步证明......
在我的web.xml 中,我正在加载我的 contextConfigLocation 和 DispatcherServlet:
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext.xml</param-value>
</context-param>
....
<servlet>
<servlet-name>ahpw-api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ahpw-api</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
....
webmvc-config.xml 仅包含允许我打开 Jackson JSON API 的基本知识:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="com.ahpw.api" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- Cookies Filter to set cookies on JSON AJAX responses -->
<mvc:interceptors>
<bean id="cookieInterceptor" class="com.ahpw.api.controller.COOKIEFilter"/>
</mvc:interceptors>
</beans>
在我的 applicationContext.xml 中,我有以下内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
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-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="com.ahpw.api" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
</context:component-scan>
<!-- Setup Jackson instance -->
<bean id="jackson" class="com.fasterxml.jackson.databind.ObjectMapper" />
<!-- Setup RabbitMQ -->
<bean id="nativeConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">
<property name="connectionTimeout" value="${rabbit.connection.timeout}"/>
<property name="requestedHeartbeat" value="${rabbit.heartbeat}"/>
</bean>
<rabbit:connection-factory
id="connectionFactory"
port="${rabbit.port}"
virtual-host="${rabbit.virtual}"
host="${rabbit.host}"
username="${rabbit.username}"
password="${rabbit.password}"
connection-factory="nativeConnectionFactory"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" reply-timeout="${rabbit.rpc.timeout}" />
</beans>
发生冲突的班级是这样开始的:
@Service
@Controller
@RequestMapping("/auth")
@JsonIgnoreProperties(ignoreUnknown = true)
public class AuthenticationController extends AbstractRPCPublisher<AuthenticationRequest, AuthenticationResponse> {
@Autowired
AmqpTemplate template;
@Autowired
ObjectMapper mapper;
如果我删除@Controller,一切正常,但显然/auth 的请求映射停止工作。
如果我把@Controller 放回去并在webmvc-config.xml 中复制jackson bean 和rabbitMQ 好东西,它启动时不会出错,但这意味着每个资源都有两个实例,即WEB-INF 和META 中的配置副本-INF,不可取。
有没有办法通过webmvc-config.xml 实例化我的控制器,但告诉它忽略@Autowired 以便我的applicationContext 可以处理它们,如果是这样,@Autowire 会正常工作吗?
【问题讨论】:
-
如果删除
@Service会发生什么?是否可以选择升级到最新版本? -
如果我删除@Service,webmvc-config 仍在尝试自动装配@Controller 注释类中的资源,并出现相同的堆栈跟踪。我宁愿推迟升级到 Spring4,除非没有其他解决方案?
-
你定义了
ContextLoaderListener监听器吗? -
这闻起来好像你实际上并没有包含你的
applicationContext.xml文件。 -
@NicolasLabrot,为我修复了它,作为答案发布,我会接受。
标签: java spring spring-mvc autowired