我正在尝试连接同一机器上的两个不同的数据库,我已经参考了here
但是我遇到了异常
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 :(得分:0)
如果有多个bean可以自动装配,则需要添加@Qualifier
注释以指定要注入的实例的名称:
@Autowired
@Qualifier("someBeanName")
private SomeType someType;
文档的相关部分:Fine-tuning annotation-based autowiring with qualifiers