Results 1 to 5 of 5

Thread: Spring Data MongoDB, is MongoTemplate a required bean?

  1. #1
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default Spring Data MongoDB, is MongoTemplate a required bean?

    So, I am creating a simple app to test all our NoSQL dbs, and in using Spring Data MongoDB, I am using repository interfaces. I am not writing any code that uses the MongoTemplate directly, so I don't understand why it seems to be a required bean.

    I get

    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personMongoDBRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' is defined
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    	at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
    	... 27 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' is defined
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    	... 44 more
    In Spring Data Neo4J, for example, I don't have to have a Neo4JTemplate bean at all.

    Thanks

    Mark

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

    Default

    as it gets auto-configured. With JPA you have to configure an EntityManagerFactory, with MongoDB you have to configure a MongoTemplate. The repositories abstraction needs an API to interact with. The required setup is also clearly documented in the reference documentation [0].

    [0] http://static.springsource.org/sprin...ngo-repo-usage

  3. #3
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Thanks Oliver.

    Your analogy of EntityManagerFactory to MongoTemplate isn't really correct. The Template classes are there to handle resources, hide boilerplate code, handle and convert exceptions and simplify an api. An EntityManagerFactory doesn't do that, and there is a JpaTemplate which is more like the MongoTemplate.

    Just like SessionFactory isn't a template either.

    Also in your Spring Data Mongo has <mongo:db-factory> which seems to me more like EntityManagerFactory than the MongoTemplate.

    It is ok that MongoTemplate is required, just odd compared to other Spring projects, it just doesn't completely copy the patterns of those other projects. Which is also a goal of Spring Data is to provide a consistent api/approach to "data access" regardless of where or how the data is stored.

    Again, I am not complaining, just pointing something out.

    Thanks

    Mark

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

    Default

    What I meant is that *every repository implementation* needs to work with an underlying API that we can execute the operations agains, that offers a query API, that does object to store mapping. An EntityManager does that, a MongoTemplate does that a Neo4jTemplate does that. The only difference is for Neo4j is that hides the template creation behind his <neo4j:config /> element. We provide a consistent programming model but there of course have to be differences in the store setup. If you look closely at the configuration options you'll see that *the only commonality* between the stores under discussion is in fact the <repositories /> element.

  5. #5
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Quote Originally Posted by Oliver Gierke View Post
    What I meant is that *every repository implementation* needs to work with an underlying API that we can execute the operations agains, that offers a query API, that does object to store mapping. An EntityManager does that, a MongoTemplate does that a Neo4jTemplate does that. The only difference is for Neo4j is that hides the template creation behind his <neo4j:config /> element. We provide a consistent programming model but there of course have to be differences in the store setup. If you look closely at the configuration options you'll see that *the only commonality* between the stores under discussion is in fact the <repositories /> element.
    Thanks. Yes it would be more like EntityManager than EntityManagerFactory.

    Can I create a Jira that would ask for the automatic creation of a MongoTemplate bean when using <mongo:db-factory>, like <neo4j:config> ? Or the creation of another tag or FactoryBean that creates them both? I know I am lazy.

    Thanks Oliver

    Mark

Posting Permissions

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