Results 1 to 6 of 6

Thread: How to instanciate MongoRepository ?

  1. #1
    Join Date
    May 2006
    Location
    Germany
    Posts
    33

    Question How to instanciate MongoRepository ?

    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.M ongoTemplate">
    <constructor-arg ref="mongo"/>
    <constructor-arg name="databaseName" value="${mongodb.dbname}"/>
    </bean>

    <mongo:repositories base-package="net.net.flexcellence.redux.server.dao.imp l" 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

    Any help appreciated.
    Regards,
    Dirk

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    493

    Default

    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

  3. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    33

    Default

    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.M ongoTemplate">
    <constructor-arg ref="mongo"/>
    <constructor-arg name="databaseName" value="${mongodb.dbname}"/>
    </bean>

    <mongo:repositories base-package="net.net.flexcellence.redux.server.dao.imp l" 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

  4. #4
    Join Date
    Apr 2010
    Posts
    24

    Default

    Code:
    <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?

  5. #5
    Join Date
    May 2006
    Location
    Germany
    Posts
    33

    Default

    You are damn right ! I thought someting like that could never happen to me. What a shame.

    Thanks a lot!

  6. #6
    Join Date
    Apr 2010
    Posts
    24

    Default

    welcome to the club

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •