Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8147485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:22:07+00:00 2026-06-06T14:22:07+00:00

I have a MongoService class public class MongoService { private final Mongo mongo; private

  • 0

I have a MongoService class

public class MongoService {

    private final Mongo mongo;
    private final String database;
    private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

    public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }

    public void putDocument(@Nonnull final DBObject document) {
        LOGGER.info("inserting document - " + document.toString());
        mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
    }

    public void putDocuments(@Nonnull final List<DBObject> documents) {
        for (final DBObject document : documents) {
            putDocument(document);
        }
    }

    @Nullable
    public <T extends DBObject> T getDocument(@Nonnull final T document) {
        final DBCollection collection = mongo.getDB(database).getCollection(getCollectionName(document));
        collection.setObjectClass(document.getClass());
        //noinspection unchecked
        return (T) collection.findOne(document);
    }

    @Nonnull
    public <T extends DBObject> List<T> getDocuments(@Nonnull final T document) {
        final List<DBObject> documents = new ArrayList<DBObject>();
        final DBCollection collection = mongo.getDB(database).getCollection(getCollectionName(document));
        collection.setObjectClass(document.getClass());
        final DBCursor dbCursor = collection.find();
        if (dbCursor != null) {
            documents.add(dbCursor.next());
        }
        //noinspection unchecked
        return (List<T>) documents;
    }
}  

I want to create a singleton bean of this class which reads the host, int, port, database from the file

Question

How shall I go about achieving this?

  • What are the best way to provide the configuration params from file
  • How do I make it singleton bean?

I am new to Spring and don’t really know how to achieve this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:22:10+00:00Added an answer on June 6, 2026 at 2:22 pm

    You can pass the values to the constructor as follows

    <bean id="mongoService" class="MongoService">
      <constructor-arg name="host" type="java.lang.String" value="localhost"/>
      <constructor-arg name="port" type="long" value="1234"/>
      <constructor-arg name="db" type="java.lang.String" value="dbname"/>
    </bean>
    

    Note: This is Spring 3 syntax, older version doesn’t support name parameter, just remove it.

    If you want to further externalize the properties, you can do this.

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations" value="jdbc.properties"/>
    </bean>
    
    <bean id="mongoService" class="MongoService">
      <constructor-arg name="host" type="java.lang.String" value="${host}"/>
      <constructor-arg name="port" type="long" value="${port}"/>
      <constructor-arg name="db" type="java.lang.String" value="${db}"/>
    </bean>
    

    Make sure jdbc.properties is in your classpath.

    Entries in jdbc.properties

    host=localhost
    port=1234
    db=dbname
    

    EDIT: For you second question, this mongoService bean is by default Singleton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following code public class MongoService { private final Mongo mongo; private
I have following situation - Have a MongoService class, which reads host, port, database
have next code: class GameTexture { private: LPDIRECT3DTEXTURE9 texture; unsigned char *alphaLayer; UINT width,
I have a following test @Test public void testPutDocuments() throws Exception { final DBObject
Have searched the database but need to specifically sum(of hours flown or days off)in
Have a xml string, goal is to replace an xml element value to a
Have some dates in my local Oracle 11g database that are in this format:
have an an array String classname[]={'a','b','c','d'}; ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>( getApplicationContext(), R.layout.spinner_item_class, R.id.spinnerclasstxt,
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
Have a string: myString = '<p>Phone Number:</p><p>706-878-8888</p>' Trying to regex out all HTML tags,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.