Question

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>

My program throws exception like

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]

where am I lacking?

Was it helpful?

Solution

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?

Sample:

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;
}

Edit: 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top