은 내가 파일에서 데이터베이스를 호스트, INT, 포트를 읽고이 클래스의 싱글 톤 bean을 작성하고자하는 MongoService 클래스를클래스를 bean으로 만드는 방법은 무엇입니까?
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;
}
}
이
질문
어떻게하여야한다 나는 이것을 성취하려고 하는가?
- 는
- 가 어떻게 그것을 싱글 콩해야합니까 파일에서 구성 PARAMS을 제공하는 가장 좋은 방법은 무엇입니까?
나는 봄에 새로운 오전 정말이
이 파일은'jdbc.properties' 이외의 다른 특성 파일 일 수 있으며 classpath에 머물러있을 수 있습니까? – daydreamer
예, PropertyPlaceholderConfigurer를 구성하여 classpath에서 특성 파일을로드 할 수 있습니다. 위의 예에서 locations 속성을 변경하십시오. – sperumal
fyi : 이름이
Matt