Search code examples
springmongodbapplicationcontext

How to get parameter/attribute from applicationContext.xml file


Application Context file contains

applicationContext.xml

<!-- Directs Java to correct Mongo DB address and port -->
<mongo:mongo host="127.0.0.1" port="27017" />
<mongo:db-factory dbname="myDb" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

Question 1 :: From within my classes, how would I retrieve mongo host="127.0.0.1" from the applicationContext.xml file?

Example:

String port = applicatoinContextObject.getValue("mongo host");

//which would return port="127.0.0.1"

This obviously doesn't work but this psuedo code is exactly what I would like to do.


Solution

  • Deepening on your spring-data-mongodb (java driver) version. You need to do something as follows:

    Mongo mongo = applicationContext.getBean(Mongo.class);
    String host = mongo.getConnectPoint();
    

    The point is that you have a Mongo typed bean is your application context.