当前正在创建请求的bean错误:带有mongoTemplate Autowire的Mongo Converter

时间:2019-03-26 07:47:38

标签: java spring mongodb spring-data-mongodb

我正在编写自定义转换器,以使用Spring Data Mongo API过滤对象并将其保存到mongodb。我试图将MongoTemplate bean注入我的Converter类,以便可以从数据库获取一些主数据。

我可以在getSequenceByName方法中获取MongoTemplate bean作为延迟加载bean,但是在这种情况下,每次创建序列时我都会获取bean(我存储了一个大的BSON文件,对于每个节点,我都将一个序列添加为u唯一键)。

<mongo:mapping-converter id="mongoConverter">
<mongo:custom-converters base-package="com.gxs.activefoundation.valueobject">
    <mongo:converter ref="documentWriteConverter"/>
</mongo:custom-converters>
</mongo:mapping-converter>

<bean id="documentWriteConverter" class="com.gxs.activefoundation.mongo.converters.RuntimeDocumentWriteConverter">
    <property name="mongoSequenceDao" ref="mongoSequenceDao"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoConverter" ref="mongoConverter" />
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>

<bean id="mongoTemplateDao" class="com.gxs.activefoundation.dao.impl.MongoTemplateDaoImpl">
    <property name="mongoOperation" ref="mongoTemplate"/>
</bean>

<bean id="mongoSequenceDao" class="com.gxs.activefoundation.dao.impl.MongoSequenceDaoImpl">
    <property name="mongoTemplateDao" ref="mongoTemplateDao"/>
</bean>


@WritingConverter
public class RuntimeDocumentWriteConverter implements Converter<RuntimeDocument, DBObject> {

private MongoSequenceDao mongoSequenceDao;

    public MongoSequenceDao getMongoSequenceDao() {
        return mongoSequenceDao;
    }

    public void setMongoSequenceDao(MongoSequenceDao mongoSequenceDao) {
        this.mongoSequenceDao = mongoSequenceDao;
    }

    @Override
    public DBObject convert(RuntimeDocument runtimeDocument) {
        DBObject dbObject = new BasicDBObject();
        getMongoSequenceDao().getSequenceByName("_zone")
        return dbObject;
    }
}

public class MongoSequenceDaoImpl implements MongoSequenceDao {

    private MongoTemplateDao mongoTemplateDao;

    public MongoTemplateDao getMongoTemplateDao() {
        return mongoTemplateDao;
    }

    public void setMongoTemplateDao(MongoTemplateDao mongoTemplateDao) {
        this.mongoTemplateDao = mongoTemplateDao;
    }

    @Override
    public Long getSequenceByName(String sequenceName) {
        //get sequence id
    }

}

如何在转换器类中执行DB READ操作。还是我需要创建多个MongoTemplate bean并将其用于转换器类?请建议,是否有更好的方法可以实现这一目标。感谢您的帮助。

0 个答案:

没有答案