代码之家  ›  专栏  ›  技术社区  ›  omega

Liberty+Spring数据中容器管理的MongoDB连接

  •  2
  • omega  · 技术社区  · 8 年前

    我们开发了一个应用程序 Spring Boot spring data (后端)+ MongoDB 使用 IBM Websphere Liberty 作为应用服务器。我们被利用了 在一个 yml 并享受了Spring Boot的好处 autoconfiguration .

    由于策略更改,我们需要在Liberty Server(使用mongo功能)中管理数据库连接 Server.xml “容器管理的MongoDB连接” IBM Websphere Liberty Server .

    有人能支持一下吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Dovmo    8 年前

    退房 this other stackoverflow solution . 下面是如何在SpringBoot应用程序中使用它的扩展。

    DelegatingDataSource .

    @Configuration
    public class DataSourceConfiguration {
    
        // This is the last code section from that link above
        @Resource(lookup = "jdbc/oracle")
        DataSource ds;
    
        @Bean
        public DataSource mySpringManagedDS() {
            return new DelegatingDataSource(ds);
        }
    
    }
    

    那么你应该可以注射 mySpringManagedDS DataSource 进入你的 Component Service 等等。

        2
  •  0
  •   Andy Guibert    7 年前

    在过去,自由有一个 mongodb-2.0 的功能服务器.xml但是,这个特性提供的好处微乎其微,因为您仍然需要自带MongoDB库。而且,随着时间的推移,MongoDB对其API进行了重大的突破性更改,包括MongoDB的配置方式。

    由于mongodbapi在不同版本之间变化如此之大,我们发现最好 不

    CDI生产者(也持有任何配置):

    @ApplicationScoped
    public 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();
        }
    }
    

    用法示例:

    @Inject
    MongoDatabase 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);
    }
    

    这只是基础知识,但下面的博文将更详细地介绍代码示例: https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html