我有一MongoService
堂课
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);
}
}
}
我想将 的值从外部属性文件/storage/local.propertieshost, port, db
注入构造函数
### === MongoDB interaction === ###
host=127.0.0.1
port=27017
database=contract
我的 Spring wireup 文件如下所示
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>
问题
如何传递host, port, db
来自local.properties
文件的值并将其传递给以下构造函数?
public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
mongo = new Mongo(host, port);
database = db;
}