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

  • Home
  • SEARCH
  • 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 8164841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:24:19+00:00 2026-06-06T19:24:19+00:00

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

  • 0

I have a MongoService class as

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);
        }
    }

}

I want to inject the value of host, port, db into constructor from a external properties file
/storage/local.properties

### === MongoDB interaction === ###
host=127.0.0.1
port=27017
database=contract  

My Spring wireup file looks as follows
wireup.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <util:properties id="mongoProperties" location="file:///storage//local.properties" />

    <bean id="mongoService" class="com.business.persist.MongoService">
        // TODO
    </bean>
</beans>

Question

How can I pass the value of host, port, db from local.properties file and pass it to the following constructor?

public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }
  • 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-06T19:24:21+00:00Added an answer on June 6, 2026 at 7:24 pm

    Instead of importing your properties file using the util:properties tag, you want to import it using the context:property-placeholder. The util version simply imports the file as a Properties object, rather than exposing the property values to your configuration. So your setup would be something like:

    <context:property-placeholder location="file:///storage//local.properties"/>
    

    Then when you are wiring up your MongoService, you can use the property names in your constructor config, such as

    <bean id="mongoService" class="com.business.persist.MongoService">
        <constructor-arg value="${host}"/>
        <constructor-arg value="${port}"/>
        <constructor-arg value="${database}"/>
    </bean>
    

    See the spring docs for more details.
    On a side note I would consider giving a more descriptive name to each of the properties to avoid collision with other properties that might be defined in your application.

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

Sidebar

Related Questions

I have a MongoService class public class MongoService { private final Mongo mongo; private
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 bean mongoService which looks like as <?xml version=1.0 encoding=UTF-8?> <beans xmlns=http://www.springframework.org/schema/beans
have written this little class, which generates a UUID every time an object of
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,

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.