working with spring-data-solr spring-data-mongodb

Problem statement

Recently I’m running into an issue when trying to add spring-data-solr into a project that’s already using spring-data-mongodb

Here is my maven/gradle dependency

dataMongo: "",

Here is my spring configuration: application-context.xml

<pre><mongo:mongo id="mongo" host="${}" port="${app.mongo.port}"/>
<bean id="mongoTemplate" class="">
    <constructor-arg ref="mongo" />
    <constructor-arg value="posts" />

<solr:solr-server id="solrServer" url="${solr.server.url}"/>
 <bean id="solrTemplate" class="">
 <constructor-arg ref="solrServer"/>

The moment I’ve added the dependency, the build is broken with the following error

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [posts.dao.ActivityTrackRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

It took me almost whole day to figure out spring-data-solr and spring-data-mongo couldn’t co-exist in one project without some workaound.

The current implementation of afterBeanDiscovery() in create bean instance for all repositories. When you use more than one Spring-Data-xxx there are multiple bean references in the context and ambigous name exception occur.

For more details, check this useful post out:

Basically, in order to make it work, you need to

  1. checkout the spring sources of spring-data-mongo
  2. add an annotation in cdi package : @MongoRepositoryCdi
  3. Annotated the mongoRepository interface with @MongoRepositoryCdi
  4. In afterBeanDiscovery method verify if @MongoRepositoryCdi is present before create the repository bean
  5. Do the same things in spring-data-solr

I ended up using SolrJ directly for solr and spring data for mongoDB.


(Visited 96 times, 1 visits today)

Leave a Reply