春季在servlet-context.xml中添加多个GridFS模板 [英] Spring adding multiple GridFS templates in servlet-context.xml
问题描述
我是Spring框架的新手.我计划在下一个项目中使用两个gridfs模板.我想使用两个不同的数据库"ProductImage"和"ProfileImage".根据用户上传的图像,应在相关数据库中插入图像.因此,我正在尝试按如下方式配置我的application-context.xml
I am new bie to Spring framework. I am planning use to use two gridfs templates for my next project. I want to use two different databases "ProductImage" and "ProfileImage". Depending on image uploaded by user image should be inserted in relevant database. So I am trying to configure my application-context.xml as follows
<!-- Mongo GridFs settings -->
<!-- Connection to MongoDB server -->
<mongo:db-factory host="192.168.1.3" port="27017"
dbname="ProfileImages" />
<mongo:mapping-converter id="converter"
db-factory-ref="mongoDbFactory" />
<!-- MongoDB GridFS Template -->
<beans:bean id="gridTemplate"
class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<beans:constructor-arg ref="mongoDbFactory" />
<beans:constructor-arg ref="converter" />
</beans:bean>
当我添加此项目时,我的项目可以正常运行,但是当我添加
When I add this my project work fine but when I add
<!-- Adding another mongo gridsfs -->
<!-- Connection to MongoDB server -->
<mongo:db-factory host="192.168.1.3" port="27017"
dbname="ProductImages" />
<mongo:mapping-converter id="ProductImages"
db-factory-ref="mongoDbFactory" />
<!-- MongoDB GridFS Template -->
<beans:bean id="gridTemplate1"
class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<beans:constructor-arg ref="mongoDbFactory" />
<beans:constructor-arg ref="ProductImages" />
</beans:bean>
我的程序抛出
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.data.mongodb.gridfs.GridFsOperations] is defined: expected single matching bean but found 2: [gridTemplate, gridTemplate1]
我在哪里缺少?
推荐答案
如果您使用的是@Autowired
批注,并且有多个候选对象(例如在您添加第二个定义bo GridFsTemplate的情况下),那么您需要使用另一个注释@Qualifier
.否则spring怎么能告诉您要注入哪个gridfs实例?
If you're using @Autowired
annotation and there are more than one good candidate (like in case where you add second definition bo GridFsTemplate), then you need to use another annotation @Qualifier
. Otherwise how spring could tell which instance of gridfs would you like to be injected?
示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
public class MyBean {
@Autowired @Qualifier("gridTemplate") private GridFsTemplate fs1;
@Autowired @Qualifier("gridTemplate1") private GridFsTemplate fs2;
}
您还需要在xml配置中正确绑定bean(您有两个mongo:db-factory bean):
You also need to bind beans properly in your xml config (you have two mongo:db-factory beans):
<!-- Mongo GridFs settings -->
<!-- Connection to MongoDB server -->
<mongo:db-factory host="192.168.1.3" port="27017"
id="profileImagesDbFactory"
dbname="ProfileImages" />
<mongo:mapping-converter id="profileImagesConverter"
db-factory-ref="profileImagesDbFactory" />
<!-- MongoDB GridFS Template -->
<beans:bean id="gridTemplate"
class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<beans:constructor-arg ref="profileImagesDbFactory" />
<beans:constructor-arg ref="profileImagesConverter" />
</beans:bean>
<!-- Adding another mongo gridsfs -->
<!-- Connection to MongoDB server -->
<mongo:db-factory host="192.168.1.3" port="27017"
id="productImagesDbFactory"
dbname="ProductImages" />
<mongo:mapping-converter id="ProductImages"
db-factory-ref="mongoDbFactory" />
<!-- MongoDB GridFS Template -->
<beans:bean id="gridTemplate1"
class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<beans:constructor-arg ref="productImagesDbFactory" />
<beans:constructor-arg ref="ProductImages" />
</beans:bean>
这篇关于春季在servlet-context.xml中添加多个GridFS模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!