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: "org.springframework.data:spring-data-mongodb:1.8.4.RELEASE",
dataSolr:"org.springframework.data:spring-data-solr:1.5.4.RELEASE"</pre>

Here is my spring configuration: application-context.xml

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

<solr:solr-server id="solrServer" url="${solr.server.url}"/>
 <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
 <constructor-arg ref="solrServer"/>
</bean></pre>

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 CdiRepositoryExtensionSupport.java 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: http://stackoverflow.com/questions/27130458/spring-data-solr-and-mongo-cdi

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