2013-12-17 105 views
1

我試圖與同MACHIN兩個不同的數據庫連接,我已經審閱here添加多個mongoTemplates到Servlet的上下文

,但我得到以下異常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'locationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.data.mongodb.core.MongoTemplate com.cheasyy.cofinding.service.profile.LocationServiceImpl.mt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.data.mongodb.core.MongoTemplate] is defined: expected single matching bean but found 2: [mongoTemplate, readTemplate] 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 

我的servlet-context.xml中貌似

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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/data/mongo 
     http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 
    <beans:bean 
     class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
     in the /WEB-INF/views directory --> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan 
     base-package="com.cheasyy.cofinding,com.cheasyy.cofinding.controller,com.cheasyy.cofinding.model.profile,com.cheasyy.cofinding.service.profile" /> 


    <!-- Mongo settings --> 
    <mongo:mongo id="mongo" host="192.168.1.3" port="27017" /> 

    <beans:bean id="mongoTemplate" 
     class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <beans:constructor-arg ref="mongo" /> 
     <beans:constructor-arg name="databaseName" 
      value="cofinding" /> 
    </beans:bean> 
    <mongo:repositories 
     base-package="com.cheasyy.cofinding,com.cheasyy.cofinding.controller,com.cheasyy.cofinding.model.profile,com.cheasyy.cofinding.service.profile" /> 

    <!-- adding another mongo template --> 
    <!-- Mongo settings --> 
    <mongo:mongo id="read" host="192.168.1.3" port="27017" /> 

    <beans:bean id="readTemplate" 
     class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <beans:constructor-arg ref="read" /> 
     <beans:constructor-arg name="databaseName" 
      value="readDB" /> 
    </beans:bean> 
    <mongo:repositories 
     base-package="com.cheasyy.cofinding,com.cheasyy.cofinding.controller,com.cheasyy.cofinding.model.profile,com.cheasyy.cofinding.service.profile" /> 




</beans:beans> 

我已經試過

@Autowired 
@Qualifier("readTemplate") 
private MongoTemplate mt1; 

@Autowired 
@Qualifier("mongoTemplate") 
private MongoTemplate mt; 

回答

0

當有一個以上的豆,可自動連接,你需要添加一個@Qualifier批註指定您希望注入的實例的名稱:

@Autowired 
@Qualifier("someBeanName") 
private SomeType someType; 

的文檔的相關部分:Fine-tuning annotation-based autowiring with qualifiers

+0

我編輯過的問題實際上我曾嘗試過,但給出了相同的例外。 –

+0

你的編輯應該工作。清理項目,重新編譯所有內容並重試。 – 2013-12-17 06:53:01

+0

仍然會出現相同的錯誤。 –