2017-03-01 5 views
1

私はMongoDBをデータベースとして使用するjava-spring Webアプリケーションを持っています。下の行は、データベースに接続するために使用されます。別のクラスにおいてMongoClientインスタンスをシングルトンにします

public class SpringMongoConfig { 
@Bean 
public MongoClient mongo() throws Exception { 
    ServerAddress serverAddress = new ServerAddress(databaseUri, databasePort); 
    List<MongoCredential> credentials = (databaseAuthenticationEnabled) ? Arrays.asList(
      MongoCredential.createCredential(databaseUser, authenticationDatabase, databasePassword.toCharArray())) 
      : null; 
    return new MongoClient(serverAddress, credentials);   
} 

}

、どのように私はこのmongoClientインスタンスを得るのだろうか?

+0

※別クラス※春豆ですか? – Eugene

+0

サービスクラスです。 – user3334226

答えて

0

私はSpringMongoConfigが実際に@Configurationひいてはでアノテートされていることを前提としています@Autowire単に、その後

@OtherConfigOfAnotherClass 
@Import(SpringMongoConfig.class) 

そして:あなたはあなたのanother classの設定と@ImportSpringMongoConfigを見つける必要がある

このような何かをサービス内のMongoClient。

+0

助けてくれてありがとう、それは働いた。 – user3334226

関連する問題