Bean实例化失败:指定的类是一个接口 [英] Instantiation of bean failed : Specified class is an interface

查看:410
本文介绍了Bean实例化失败:指定的类是一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建用于依赖项注入的bean时遇到问题.这是场景.

I am facing an issue while creating a bean for dependency injection. Here is the scenario.

我正在处理MongoDB存储库,我还创建了一个使用它的类.我正在尝试实例化两者的bean实例.

I am dealing with MongoDB repository, I have also created a class which uses it. I am trying to instantiate bean instance of both.

MongoDB备案:

MongoDB reporsitory:

@Repository
public interface ProductGlobalTrendRepository extends MongoRepository<ProductGlobalTrend,String>{
    public ProductGlobalTrend findByPid(@Param("pId") String pId);
}

使用它的类:

@Service
@Scope("singleton")
public class ProductTrendService {

    @Autowired
    @Qualifier("productGlobalTrendRepo")
    ProductGlobalTrendRepository productGlobalTrendRepo;

    public ProductTrendService() {
        super();
    }   

    public void setProductGlobalTrendRepo(
            ProductGlobalTrendRepository productGlobalTrendRepo) {
        this.productGlobalTrendRepo = productGlobalTrendRepo;
    }

    public ProductTrendService(ProductGlobalTrendRepository productGlobalTrendRepo) {
        super();
        this.productGlobalTrendRepo = productGlobalTrendRepo;
    }
}   

spring的bean config xml具有以下条目:

The spring's bean config xml has these entries:

<bean id="productTrendService" class="com.api.services.ProductTrendService"> </bean>
<bean id="productGlobalTrendRepo" class="com.mongodb.repository.ProductGlobalTrendRepository"> </bean>

以下是我得到的错误:

19428 [localhost-startStop-1]警告 org.springframework.web.context.support.AnnotationConfigWebApplicationContext -上下文初始化期间遇到异常-取消刷新尝试 org.springframework.beans.factory.BeanCreationException:错误 创建名称为"productTrendService"的bean:自动接线的注入 依赖项失败;嵌套的异常是 org.springframework.beans.factory.BeanCreationException:无法 自动连线栏位:com.mongodb.repository.ProductGlobalTrendRepository com.api.services.ProductTrendService.productGlobalTrendRepo;嵌套的 异常是org.springframework.beans.factory.BeanCreationException: 创建名称为"productGlobalTrendRepo"的bean时出错 类路径资源[com/vstore/conf/spring-security.xml]: 实例化bean失败;嵌套的异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[com.mongodb.repository.ProductGlobalTrendRepository]: 指定的类是接口

19428 [localhost-startStop-1] WARN org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTrendService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mongodb.repository.ProductGlobalTrendRepository com.api.services.ProductTrendService.productGlobalTrendRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productGlobalTrendRepo' defined in class path resource [com/vstore/conf/spring-security.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.repository.ProductGlobalTrendRepository]: Specified class is an interface

它抱怨存储库是一个接口类.

It complains that repository is a interface class.

有人可以为这个bean依赖注入建议一个修复/解决方法吗?

Can somebody please suggest a fix/workaround for this bean dependency injection ?

推荐答案

问题在于上下文文件中的以下信息

The problem is with the following information in your context file

 <bean id="productGlobalTrendRepo"  
        class="com.mongodb.repository.ProductGlobalTrendRepository"> 
 </bean>

您应该创建com.mongodb.repository.ProductGlobalTrendRepositoryImpl类,该类实现com.mongodb.repository.ProductGlobalTrendRepository并提供其方法的实现.

You should create a class com.mongodb.repository.ProductGlobalTrendRepositoryImpl which implements com.mongodb.repository.ProductGlobalTrendRepository and provides implementation of its methods.

然后将您的bean声明信息更改为

then change your bean declaration info as

  <bean id="productGlobalTrendRepo"   
     class="com.mongodb.repository.ProductGlobalTrendRepositoryImpl">    
  </bean>

在后台创建对象,而该对象在界面中是不可能的.

Behind the scene the object is created which is not possible with the interface.

这篇关于Bean实例化失败:指定的类是一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆