spring data mongodb自定义接口ConflictingBeanDefinitionException

时间:2014-08-08 13:42:50

标签: spring spring-data spring-data-mongodb

按照Spring Data MongoDB手册,我创建了存储库和实现类。但是,当我启动我的应用程序时,spring bootstrap失败并带有

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'mockTestRepositoryImpl' for bean class [com.foo.testapp.mongo.repository.impl.MockTestRepositoryImpl] conflicts with existing, non-compatible bean definition of same name and class [com.foo.testapp.mongo.repository.impl.MockTestRepositoryImpl]

我的配置和代码:(groovy)

package com.foo.testapp.mongo.repository.impl

@Component
class MockTestRepositoryImpl implements MockTestRepositoryCustom {
    MongoOperations mongoOperations   
    @Autowired
    MockTestRepositoryImpl(MongoOperations mongoOperations) {
        this.mongoOperations = mongoOperations
    }   
    @Override
    MockTest findAndModify(Query query, Update update, FindAndModifyOptions options) {
        return mongoOperations.findAndModify(query, update, options, MockTest)
    }   
    @Override
    boolean exists(Query query) {
        return mongoOperations.exists(query, MockTest)
    }
}

自定义界面:

package com.foo.testapp.mongo.repository
public interface MockTestRepositoryCustom {
    MockTest findAndModify(Query query, Update update, FindAndModifyOptions options)
    boolean exists(Query query);
}

Spring Data存储库:

package com.foo.testapp.mongo.repository
public interface MockTestRepository extends MongoRepository<MockTest, String>, MockTestRepositoryCustom {
List<MockTest> findByDeleted(boolean deleted);
    MockTest findByIdAndDeleted(String id, boolean deleted);
    @Query(value = "{ 'deleted' : ?0 }", fields = "{ 'name' : 1 }")
    List<MockTest> findAllBasicInfo(boolean deleted);
}

1 个答案:

答案 0 :(得分:2)

问题是您注释了MockTestRepositoryImpl @Component

您需要完全删除@Component(正如您在手册中看到的那样,没有这样的注释 - 但所有Spring功能都能正常工作)。