Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Plain Simple MongoDB Spring Integration

DZone's Guide to

Plain Simple MongoDB Spring Integration

· Database Zone ·
Free Resource

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

You know what is MongoDB and what is Spring Framework and want to use the first inside the second? Here's short plain and simple integration between the two:

  • Properties file with server and database details (resides in classpath in this example):
db.host=localhostdb.port=27017app.db.name=app
  1. application-config.xml (or whatever you call it):
    <beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:property-placeholder                 location="classpath:db.properties"/>    <bean id="mongo" class="com.mongodb.Mongo">       <constructor-arg value="${db.host}"/>       <constructor-arg value="${db.port}"/>   </bean>   <bean id="db"       class="com.mongodb.spring.config.DbFactoryBean">       <property name="mongo" ref="mongo"/>       <property name="name" value="${app.db.name}"/>   </bean></beans>
  2. The com.mongodb.spring.config.DbFactoryBean class:
     public class DbFactoryBean implements FactoryBean<DB> {            private Mongo mongo;        private String name;            @Override        public DB getObject() throws Exception {            Assert.notNull(mongo);            Assert.notNull(name);            return mongo.getDB(name);        }          @Override       public Class<?> getObjectType() {           return DB.class;       }          @Override       public boolean isSingleton() {           return true;       }          @Required       public void setMongo(Mongo mongo) {           this.mongo = mongo;       }       @Required          public void setName(String name) {           this.name = name;       } }
    @Configuration    public class ApplicationConfiguration {            @Value("${app.db.name}")        private String appDbName;            @Value("${db.host}")        private String dbHost;           @Value("${db.port}")       private int dbPort;             @Bean       public DB db() throws UnknownHostException {           return mongo().getDB(appDbName);       }          @Bean       public Mongo mongo() throws UnknownHostException {           return new Mongo(dbHost, dbPort);       }   }

That's, actually, it - enjoy. If you feel some part of the puzzle is missing, please leave a comment.

This post was originally posted in my blog - http://jbaruch.wordpress.com

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

Topics:
java ,nosql ,enterprise-integration ,spring ,mongodb

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}