有人可以在这里支持吗? 解决方案 过去 Liberty 为 server.xml 提供了一个专用的 mongodb-2.0 特性,但是这个特性提供的好处非常小,因为您仍然需要带上自己的 MongoDB 库.此外,随着时间的推移,MongoDB 对其 API 进行了重大的突破性更改,包括 MongoDB 的配置方式.由于 MongoDB API 在不同版本之间变化如此之大,我们发现最好不在 Liberty 中提供任何新的 MongoDB 功能,而是建议用户简单地使用这样的 CDI 生成器:CDI 生产者(也持有任何配置):@ApplicationScoped公共类 MongoProducer {@Produces公共 MongoClient createMongo() {return new MongoClient(new ServerAddress(), new MongoClientOptions.Builder().build());}@Produces公共 MongoDatabase createDB(MongoClient 客户端){返回 client.getDatabase("testdb");}public void close(@Disposes MongoClient toClose) {toClose.close();}}示例用法:@InjectMongoDatabase db;@POST@Path("/添加")@Consumes(MediaType.APPLICATION_JSON)公共无效添加(船员成员船员成员){MongoCollection船员 = db.getCollection("船员");文档 newCrewMember = new Document();newCrewMember.put("Name",crewMember.getName());newCrewMember.put("Rank",crewMember.getRank());newCrewMember.put("CrewID",crewMember.getCrewID());船员.insertOne(newCrewMember);}这只是基础知识,但以下博客文章更详细地介绍了代码示例:https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.htmlWe have developed an application in Spring Boot + spring data (backend) + MongoDB and used IBM Websphere Liberty as application Server. We were used "Application Managed DB Connection" in an yml file and enjoyed the benefit of Spring Boot autoconfiguration.Due to policy changes, we would need to manage our DB Connection in Liberty Server(using mongo feature), in Server.xml. I spent whole day in finding out an good example to do this, but dont find any example in Spring with "Container Managed MongoDB Connection" in IBM Websphere Liberty Server.Can someone please support here? 解决方案 In the past Liberty had a dedicated mongodb-2.0 feature for the server.xml, however this feature provided pretty minimal benefit, since you still needed to bring your own MongoDB libraries. Also, over time MongoDB made significant breaking changes to their API, including how MongoDB gets configured.Since the MongoDB API is changing so drastically between releases, we found it better to not provide any new MongoDB features in Liberty and instead suggest that users simply use a CDI producer like this:CDI producer (holds any configuration too):@ApplicationScopedpublic class MongoProducer { @Produces public MongoClient createMongo() { return new MongoClient(new ServerAddress(), new MongoClientOptions.Builder().build()); } @Produces public MongoDatabase createDB(MongoClient client) { return client.getDatabase("testdb"); } public void close(@Disposes MongoClient toClose) { toClose.close(); }}Example usage:@InjectMongoDatabase db;@POST@Path("/add")@Consumes(MediaType.APPLICATION_JSON)public void add(CrewMember crewMember) { MongoCollection<Document> crew = db.getCollection("Crew"); Document newCrewMember = new Document(); newCrewMember.put("Name",crewMember.getName()); newCrewMember.put("Rank",crewMember.getRank()); newCrewMember.put("CrewID",crewMember.getCrewID()); crew.insertOne(newCrewMember);}This is just the basics, but the following blog post goes into much greater detail along with code examples:https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html 这篇关于Liberty + Spring Data 中的容器管理的 MongoDB 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 17:36