如果这对任何人都有帮助,以下方法是有效的!!
创建一个新工厂
@Replaces
允许重写MongoClient的默认注入的注释。在这个类中,从加密存储读取凭证,然后使用相同的凭证来构造Mongo连接字符串
@Factory
public class MongoClientFactory {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Inject EncryptedStoreService encryptedStoreService;
@Singleton
@Bean(preDestroy = "close")
@Replaces(MongoClient.class)
public MongoClient mongoClient() {
MongoClient mongoClient = null;
try {
// Formulate a ConnectionString based on data read using the encryptedStoreService
mongoClient = MongoClients.create(connectionString);
} catch (IOException e) {
logger.atSevere().log("Error occured", e);
}
return mongoClient;
}
}
爱Micronaut!!