gpt4 book ai didi

java - 如何使用 spring 创建和配置内部构建器类

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:47 24 4
gpt4 key购买 nike

我正在尝试 Spring 化以下代码片段:

MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
DB db = mongoClient.getDB("jcr");
DocumentNodeStore ns = new DocumentMK.Builder().setMongoDB(db)
.getNodeStore();
Repository repo = new Jcr(new Oak(ns)).createRepository();

取自Oak网站:http://jackrabbit.apache.org/oak/docs/construct.html

问题行是:

DocumentNodeStore ns = new DocumentMK.Builder().setMongoDB(db)
.getNodeStore();

以下是我在 xml 中配置它的方法:

<bean id="builder" class="org.apache.jackrabbit.oak.plugins.document.DocumentMK$Builder">   
</bean>

<bean factory-bean="builder" factory-method="setMongoDB" >
<constructor-arg name="db" value="#{mongoDbTags.getDb()}"/>
</bean>

我已经配置了一个 SimpleMongoDbFactory 并获取要注入(inject)到通过工厂方法调用 setMethod 的构建器 bean 中的数据库。

请注意,这并不作为构建器类下的属性存在,但该方法确实存在。

这是我的配置文件:

    <bean id="mongo" class="com.mongodb.MongoClient">
<constructor-arg name="host" value="localhost" />
<constructor-arg name="port" value="27017" />
</bean>
<bean id="mongoDbTags" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="jcr111" />
</bean>
<bean id="builder" class="org.apache.jackrabbit.oak.plugins.document.DocumentMK$Builder">
</bean>

<bean factory-bean="builder" factory-method="setMongoDB" >
<constructor-arg name="db" value="#{mongoDbTags.getDb()}"/>
</bean>
<bean id="documentMK" class="org.apache.jackrabbit.oak.plugins.document.DocumentMK">
<constructor-arg name="builder" ref="builder" />
</bean>

<bean id="oak" class="org.apache.jackrabbit.oak.Oak">
<constructor-arg name="store" value="#{builder.getNodeStore()}" />
</bean>

<bean id="jcr" class="org.apache.jackrabbit.oak.jcr.Jcr">
<constructor-arg name="oak" ref="oak" />
</bean>
<bean id="jcrSessionFactory"
class="org.springframework.extensions.jcr.JcrSessionFactory">
<property name="repository" value="#{jcr.createRepository()}" />
<property name="credentials">
<bean class="javax.jcr.SimpleCredentials">
<constructor-arg index="0" value="admin" />
<constructor-arg index="1" value="admin" />
</bean>
</property>
</bean>

<bean id="jcrTemplate" class="org.springframework.extensions.jcr.JcrTemplate">
<property name="sessionFactory" ref="jcrSessionFactory" />
<property name="allowCreate" value="true" />
</bean>

最佳答案

对于其他陷入此困境的人,我只是创建了一个包装类,该类从其构造函数中调用构建器

关于java - 如何使用 spring 创建和配置内部构建器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com