2

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

daydreamer
  • 87,243
  • 191
  • 450
  • 722

2 Answers2

4

Spring beans are singleton by default so..

@Service
public class MongoService

Check out How can I inject a property value into a Spring Bean which was configured using annotations? for injecting properties, it's pretty nifty.

Community
  • 1
  • 1
Rasmus Franke
  • 4,434
  • 8
  • 45
  • 62
1

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.

sperumal
  • 1,499
  • 10
  • 14