Announcement Announcement Module
Collapse
No announcement yet.
How to instanciate MongoRepository ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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

      Comment


      • #4
        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?

        Comment


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

          Thanks a lot!

          Comment


          • #6
            welcome to the club

            Comment

            Working...
            X