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>