Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: how pass values to constructor from properties file

Tags:

java

spring

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;
    }
like image 788
daydreamer Avatar asked Jul 03 '12 13:07

daydreamer


1 Answers

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.

like image 185
Mike Clark Avatar answered Sep 21 '22 10:09

Mike Clark