[英]Spring attempting to @Autowire my dependencies twice
我有一個Spring MVC應用程序,其中我試圖在帶有@Controller注釋的類中使用@Autowired。 在沒有@Controller批注的類中,@Autowired可以正常工作,一旦我添加@Controller批注,就會在啟動時獲得巨大的No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] found for dependency
其主要歸結為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是否可以正常運行?
您必須定義ContextLoaderListener
偵聽器,該偵聽器將創建Web應用程序的根上下文並在<context-param>
標記中加載contextConfigLocation
定義:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
因此,在applicationContext.xml
程序上下文中定義的bean將可用於webmvc-config.xml
應用程序上下文。
我不知道這是否是一個錯字,但是AuthenticationController
類一定不能用@Service
注釋, @Service
@Controller
就足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.