PDA

View Full Version : How to instanciate MongoRepository ?



dirk.dinger
Apr 29th, 2011, 05:49 AM
Hi Spring data users,

Just started playing around with spring data and mongodb. Following the instructions in the reference doc I created a repository interface like this

package net.flexcellence.redux.server.dao.impl;

import net.flexcellence.redux.server.dto.RedUXAsset;

import org.springframework.data.document.mongodb.reposito ry.MongoRepository;

public interface AssetRepository extends MongoRepository<RedUXAsset, String>{}


in my applicationcontext.xml I imported the mongo namespace and added the following:

<bean id="mongo" class="com.mongodb.Mongo">
<constructor-arg value="${mongodb.host}"/>
<constructor-arg value="${mongodb.port}"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTem plate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="${mongodb.dbname}"/>
</bean>

<mongo:repositories base-package="net.net.flexcellence.redux.server.dao.impl" mongo-template-ref="mongoTemplate" />

If I now write a simple unit test, based upon the repository having an autowired property like

@Autowired
private AssetRepository repository;

The Repository is not created and injected. Is there anything I am missing in my config ? Maybe aspectJ is required ?

There is a section in the reference doc "Example 4.6. Standalone usage of repository factory"
but the actual code to create the repositoy programmatically is ommitted :confused:

Any help appreciated.
Regards,
Dirk

Oliver Gierke
May 1st, 2011, 11:52 AM
Just trying to dig what you're actually trying to do. In your integration test, how do you bootstrap the container? What is the exception you get?

If you really want to instantiate the factory yourself MongoRepositoryFactory is the class you're looking for. Inside a Spring app (and thus inside Spring integration contexts we suggest usage of the namespace) so you are on the right track actually. I suspect there's something wrong in the ApplicationCOntext creation of your test setup. WOuld be helpful to know how you actually bootstrap the container.

Cheers,
Ollie

dirk.dinger
May 1st, 2011, 03:06 PM
Hello Oliver,

The context is actually setup from within a junit test like so:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/dao-test-context.xml"})
public class AssetDaoTest {

@Autowired
private AssetDao assetDao;

Within the dao-test-context I do the following

<context:component-scan base-package="net.flexcellence.redux.server"/>

<import resource="classpath:dao-context.xml"/>

and within dao-context I have

<bean id="mongo" class="com.mongodb.Mongo">
<constructor-arg value="${mongodb.host}"/>
<constructor-arg value="${mongodb.port}"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTem plate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="${mongodb.dbname}"/>
</bean>

<mongo:repositories base-package="net.net.flexcellence.redux.server.dao.impl" mongo-template-ref="mongoTemplate" />

Finally the AssetDaoImpl is found via component scanning, that actually requires the MongoRepository:

@Service
public class AssetDaoImpl2 extends AssetDaoImpl implements InitializingBean {

@Autowired
private MongoOperations mongoOps;

@Autowired
private AssetRepository repository;


Upon creation of the context the root cause of the exception that gets thrown is
Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [net.flexcellence.redux.server.dao.impl.AssetReposi tory]

Hope this provides you with the information you need.
Regards,
Dirk

chiwi
May 5th, 2011, 07:04 AM
<mongo:repositories base-package="net.net.flexcellence.redux.server.dao.imp l" mongo-template-ref="mongoTemplate" />

you are referring "net.net.flexcellence....", isn't that a typo?

dirk.dinger
May 5th, 2011, 08:46 AM
You are damn right ! I thought someting like that could never happen to me. What a shame.

Thanks a lot!

chiwi
May 5th, 2011, 08:49 AM
welcome to the club :)