spring 无法实例化 bean 类:指定的类是一个接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26721583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:11:15  来源:igfitidea点击:

Could not instantiate bean class: Specified class is an interface

spring

提问by Vaibhav

I know there are threads similar to this issue. Below is my class and I am configuring it in spring.xml file. Actually HumanResourceService is an interface having only one method.

我知道有与此问题类似的线程。下面是我的类,我在 spring.xml 文件中配置它。实际上 HumanResourceService 是一个只有一个方法的接口。

@Endpoint
public class HolidayEndpoint {

    @Autowired
    private HumanResourceService humanResourceService;

    @Autowired
    public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
        this.humanResourceService = humanResourceService;
    }
}

My problem is that in my spring.xml file, when I define HumanResourceService as bean, it cannot be instantiated as this is an interface. How can I mention an interface in spring configuration file. My spring.xml file is below

我的问题是在我的 spring.xml 文件中,当我将 HumanResourceService 定义为 bean 时,它无法实例化,因为这是一个接口。如何在 spring 配置文件中提及接口。我的 spring.xml 文件在下面

<bean id="holidayEndpoint" class="com.mycompany.hr.ws.HolidayEndpoint" autowire="constructor" >
     <property name="humanResourceService" ref="humanResourceService" />
</bean>
<bean id="humanResourceService" class="com.mycompany.hr.service.HumanResourceService" />

采纳答案by Nathan Hughes

You can't, Spring needs something it can make an instance from, the interface isn't enough.

你不能,Spring 需要一些可以从中创建实例的东西,接口是不够的。

In your spring.xml, the value of the class attribute for your bean with id="humanResourceService" should be the name of your implementation class, not the interface. Spring needs you to tell it what implementation class you want it to use for this.

在 spring.xml 中,id="humanResourceService" bean 的类属性值应该是实现类的名称,而不是接口。Spring 需要您告诉它您希望它为此使用哪个实现类。