2012-06-27 4 views
2

은 내가 파일에서 데이터베이스를 호스트, 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을 제공하는 가장 좋은 방법은 무엇입니까?

나는 봄에 새로운 오전 정말이

답변

1

당신은 생성자에 값을 전달할 수 있습니다 :이 봄 3 구문, 이전 버전은 단지 그것을 제거, name 매개 변수를 지원하지 않습니다이다.

속성을 추가로 외부화하려면이 작업을 수행 할 수 있습니다.

<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> 

jdbc.properties가 클래스 경로에 있는지 확인하십시오.

항목은 jdbc.properties

host=localhost 
port=1234 
db=dbname 

편집에 : 당신이 두 번째 질문의 경우,이 mongoService 빈은 기본 싱글입니다.

+0

이 파일은'jdbc.properties' 이외의 다른 특성 파일 일 수 있으며 classpath에 머물러있을 수 있습니까? – daydreamer

+0

예, PropertyPlaceholderConfigurer를 구성하여 classpath에서 특성 파일을로드 할 수 있습니다. 위의 예에서 locations 속성을 변경하십시오. – sperumal

+0

fyi : 이름이 Matt

4

봄 콩이 그렇게 기본적으로 싱글이다 달성하는 방법을 모르는 .. 속성을 주입하기위한

@Service 
public class MongoService 

체크 아웃 How can I inject a property value into a Spring Bean which was configured using annotations?, 그것은 꽤입니다 맵시 있는.

<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> 

참고를 다음과 같이

관련 문제