![](/img/trans.png)
[英]Creating spring bean in xml configuration for an interface without concrete implementation
[英]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)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.