MongoDB---配置多数据源报错expected single matching bean but found 2(1)
- UID
- 1066743
|
MongoDB---配置多数据源报错expected single matching bean but found 2(1)
MongoDB需要配置多数据源 配置如下:
spring-mongodb.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:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:webconfig.properties"></property>
</bean>
<mongo:db-factory id="mongoDbFactory"
host="${mongodb.host.test}"
port="${mongodb.port.test}"
dbname="${mongodb.dbname.test}"
username="${mongo.auth.username}"
password="${mongo.auth.password}"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<mongo:db-factory id="mongoDbFactoryData"
host="${mongodb.host.data}"
port="${mongodb.port.data}"
dbname="${mongodb.dbname.data}"
username="${mongo.auth.username}"
password="${mongo.auth.password}"/>
<bean id="mongoTemplateData" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactoryData"/>
</bean>
</beans>
webconfig.properties的内容为:
mongodb.host.data=192.168.30.249
mongodb.port.data=28719
mongodb.dbname.data=datamofang
mongo.auth.username=
mongo.auth.password=
mongodb.host.test=192.168.30.249
mongodb.port.test=28719
mongodb.dbname.test=testdb
这里配置了两个库作为数据源,一个是data一个是test。
mongoTemplate 和 mongoTemplateDate 都实现了org.springframework.data.mongodb.core.MongoTemplate接口。
在class中使用的注入方式如下:
@Autowired
MongoTemplate mongoTemplate;
@Autowired
MongoTemplate mongoTemplateData;
有两个bean同时实现一个接口,这两个接口注入时报了异常。
No unique bean of type is defined。
expected single matching bean but found 2. |
|
|
|
|
|