Instantiation of bean failed : Specified class is an interface

牧云@^-^@ 提交于 2019-12-12 13:32:47

问题


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

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 reporsitory:

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

The class which is using it:

@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;
    }
}   

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>

Following is the error I am getting:

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.

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


回答1:


The problem is with the following information in your context file

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

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

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.



来源:https://stackoverflow.com/questions/30795617/instantiation-of-bean-failed-specified-class-is-an-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!