繁体   English   中英

用于存储库接口的Spring Data MongoDb bean XML配置

[英]Spring Data MongoDb bean xml configuration for repository interface

我有一个使用xml配置的现有spring应用程序。 现在,我将使用spring-data-mongodb将其连接到Mongo数据库。 我的repository/dao都是类似的接口:

public interface CustomerDao extends MongoRepository<Customer, String> {
   ...
}

在我的服务类CustomerService它会自动装配CustomerDao接口。

<bean id="customerDao" class="com.myapp.repository.CustomerDao" />
<bean id="customerService" class="com.myapp.service.CustomerService">
    <property name="customerDao" ref="customerDao"/>
</bean>

但是由于CustomerDao是一个接口,所以我总是会收到错误消息:

org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myapp.repository.CustomerDao]: Specified class is an interface

基于spring-data-mongodb存储库的教程,大部分都是扩展到MongoRepository接口。

我的问题是,如果我不会在xml配置中创建bean条目,则在CustomerService类内部自动装配CustomerDao时会出错。 以下是我遇到的错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myapp.repository.CustomerDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=customerDao)}

您可以仅指定存储库包的位置 (以及自定义实现,如果需要)。

然后,您可以创建一个扩展mongo-spring-data资料库之一的接口 (我更喜欢PagingAndSortignRepository)

在此之后,你就可以自动装配您的回购协议。

别忘了检查组件扫描程序包 -您的存储库和服务应该在那里。

最后一件事-检查服务上的Spring批注

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM