如何在Spring Boot中初始化MongoClient一次并使用其方法? [英] How to initialize MongoClient once in Spring Boot and use its methods?
问题描述
你好,我试图在Spring Boot中成功连接后导出 MongoClient
,并且试图在其他文件中使用它,这样我就不必每次都调用连接了需要在我的MongoDB数据库中进行更改.
Hello I am trying to export the MongoClient
after a successful connection in Spring Boot and I am trying to use it in other files so that I do not have to call the connection every single time that I need to make changes in my MongoDB database.
连接非常简单,但是目标是将应用程序连接到我的数据库一次,然后将其导入任何Java文件中,然后在需要的地方使用它.
The connection is pretty simple but the goal would be to connect the application to my database once and then use it wherever I want by importing it in any Java file.
谢谢
推荐答案
以下是创建 MongoClient
实例,在Spring Boot应用程序中配置和使用它的几种方法.
Here are couple of ways of creating an instance of MongoClient
, configuring and using it within the Spring Boot application.
(1)使用基于Java的元数据注册Mongo实例:
@Configuration
public class AppConfig {
public @Bean MongoClient mongoClient() {
return MongoClients.create();
}
}
使用 CommandLineRunner
的 run
方法(所有示例均以类似方式运行):
@Autowired
MongoClient mongoClient;
// Retrieves a document from the "test1" collection and "test" database.
// Note the MongoDB Java Driver API methods are used here.
private void getDocument() {
MongoDatabase database = client.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("test1");
Document myDoc = collection.find().first();
System.out.println(myDoc.toJson());
}
(2)使用AbstractMongoClientConfiguration类进行配置并与MongoOperations一起使用:
@Configuration
public class MongoClientConfiguration extends AbstractMongoClientConfiguration {
@Override
public MongoClient mongoClient() {
return MongoClients.create();
}
@Override
protected String getDatabaseName() {
return "newDB";
}
}
请注意,您可以设置可以连接的数据库名称( newDB
).此配置用于通过Spring Data MongoDB API与MongoDB数据库一起使用: MongoOperations
(及其实现 MongoTemplate
)和 MongoRepository
.
Note you can set the database name (newDB
) you can connect to. This configuration is used to work with MongoDB database using Spring Data MongoDB APIs: MongoOperations
(and its implementation MongoTemplate
) and the MongoRepository
.
@Autowired
MongoOperations mongoOps;
// Connects to "newDB" database, and gets a count of all documents in the "test2" collection.
// Uses the MongoOperations interface methods.
private void getCollectionSize() {
Query query = new Query();
long n = mongoOps.count(query, "test2");
System.out.println("Collection size: " + n);
}
(3)使用AbstractMongoClientConfiguration类进行配置并与MongoRepository一起使用
使用相同的配置 MongoClientConfiguration
类(主题2中的上的),但还要使用 @EnableMongoRepositories
进行注释.在这种情况下,我们将使用 MongoRepository
接口方法将收集数据作为Java对象获取.
Using the same configuration MongoClientConfiguration
class (above in topic 2), but additionally annotate with @EnableMongoRepositories
. In this case we will use MongoRepository
interface methods to get collection data as Java objects.
存储库:
@Repository
public interface MyRepository extends MongoRepository<Test3, String> {
}
代表 test3
集合文档的 Test3.java
POJO类:
The Test3.java
POJO class representing the test3
collection's document:
public class Test3 {
private String id;
private String fld;
public Test3() {
}
// Getter and setter methods for the two fields
// Override 'toString' method
...
}
以下获取文档并作为Java对象打印的方法:
The following method to get documents and print as Java objects:
@Autowired
MyRepository repository;
// Method to get all the `test3` collection documents as Java objects.
private void getCollectionObjects() {
List<Test3> list = repository.findAll();
list.forEach(System.out::println);
}
这篇关于如何在Spring Boot中初始化MongoClient一次并使用其方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!