Enterprise Integration Zone is brought to you in partnership with:

Baruch Sadogursky, recently joined JFrog as the Developers Advocate following years of working alongside JFrog’s founding team. Prior to joining JFrog, Baruch was an innovations expert with BMC Software Incubator team after 6 years with AlphaCSP as a senior Java consultant, architect and training division manager. Baruch is hacking around Java technologies and Continuous-Integration tools since 2001, including module development for open source projects like Gradle & Spring. Baruch is also active in community development around Artifactory, participating in the development of it’s plugin ecosystem and enriching it’s functionality with open-source user plugins. As JFrog’s Developers Advocate, Baruch contributes to the strong collaboration with leading open-source projects such as SpringSource, Grails and Gradle by providing them with the Artifactory Cloud platform, and fuels the Continuous-Integration ecosystem with open-source plugins for leading tools such as Jenkins, TeamCity & Bamboo. Baruch blogs at http://blogs.jfrog.org & blog.sadogursky.com and tweets as @jbaruch. Baruch is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Plain Simple MongoDB Spring Integration

  • submit to reddit

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):
  1. application-config.xml (or whatever you call it):
    <beans xmlns="http://www.springframework.org/schema/beans"
    <bean id="mongo" class="com.mongodb.Mongo">
    <constructor-arg value="${db.host}"/>
    <constructor-arg value="${db.port}"/>
    <bean id="db"
    <property name="mongo" ref="mongo"/>
    <property name="name" value="${app.db.name}"/>
  2. The com.mongodb.spring.config.DbFactoryBean class:
     public class DbFactoryBean implements FactoryBean<DB> {

    private Mongo mongo;
    private String name;

    public DB getObject() throws Exception {
    return mongo.getDB(name);

    public Class<?> getObjectType() {
    return DB.class;

    public boolean isSingleton() {
    return true;

    public void setMongo(Mongo mongo) {
    this.mongo = mongo;

    public void setName(String name) {
    this.name = name;
public class ApplicationConfiguration {

private String appDbName;

private String dbHost;

private int dbPort;

public DB db() throws UnknownHostException {
return mongo().getDB(appDbName);

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

Published at DZone with permission of Baruch Sadogursky, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Steve Keylin replied on Sun, 2010/05/30 - 4:03pm

Lovely :-)

Andy Jefferson replied on Mon, 2010/05/31 - 3:59am

And you could take it further and use JPA/JDO with MongoDB using


Baruch Sadogursky replied on Mon, 2010/05/31 - 5:01am in response to: Andy Jefferson

Why would I want to work with non-relational database using relational API?

Manuel Jordan replied on Mon, 2010/05/31 - 2:35pm

BTW guys, Apress are in process to publish a book about Mongo http://www.apress.com/book/view/1430230517


Andy Jefferson replied on Mon, 2010/05/31 - 2:58pm in response to: Baruch Sadogursky

Which part of JDO is "relational" exactly ? It was designed as datastore agnostic. Calling pm.makePersistent() is about as agnostic as it can get. For this exact reason, many OODBMS offer it as an API to their products

Baruch Sadogursky replied on Tue, 2010/06/01 - 2:21am in response to: Andy Jefferson

You mentioned both JPA and JDO. JPA is for RDBMS only,while JDO is not. My answer was about JPA, while you right about JDO.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.